How do I subscribe to events in PencilBlue?

57 views Asked by At

I'm actually having trouble figuring out how to subscribe to events, or at least, doing so correctly. There doesn't seem to be much documentation on how to do so, so I took some cues from existing services.

Here's the code I'm working with:

module.exports = function(pb){
  //pb dependencies
  var BaseObjectService = pb.BaseObjectService;

  var TYPE = 'page';

  function PageProxyService() {}

  PageProxyService.init = function(cb){
    pb.log.debug('PageProxyService: Initialized');
    cb(null, true);
  };

  PageProxyService.handlePageSave = function(context, cb){
    // I'm using console.log to make the message stand out more.
    // For production things, I use pb.log.debug :)
    console.log("===================================");
    console.log("I GOT A CALL");
    console.log("===================================");
    console.log(context);
    console.log("===================================");
    cb(null);
  };

  // Trying to subscribe to any of these seems to do nothing.
  BaseObjectService.on(TYPE + '.' + BaseObjectService.BEFORE_SAVE, PageProxyService.handlePageSave);
  BaseObjectService.on(TYPE + '.' + BaseObjectService.AFTER_SAVE, PageProxyService.handlePageSave);

  //exports
  return PageProxyService;
};

handlePageSave doesn't ever get called. What am I doing wrong?

1

There are 1 answers

1
user3476274 On BEST ANSWER

The PageObjectService will fire events. However, as of 0.4.1 not all controllers, as you discovered, have been converted over to leverage the service. A new controller, PageApiController, has been created to take the place of the existing controller. The UI will eventually (~Q1 2016) be converted over to use the new API end-points.