我实际上很难弄清楚如何订阅事件,或者至少是正确地订阅事件。似乎没有太多关于如何做到这一点的文档,所以我从现有服务中获取了一些线索。
这是我正在使用的代码:
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
永远不会被调用。我究竟做错了什么?