【发布时间】:2015-10-17 16:53:21
【问题描述】:
我实际上在弄清楚如何订阅事件时遇到了麻烦,或者至少在正确地订阅事件时遇到了麻烦。似乎没有太多关于如何做到这一点的文档,所以我从现有服务中获取了一些线索。
这是我正在使用的代码:
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 永远不会被调用。我做错了什么?
【问题讨论】:
-
我找到了我的事件监听器没有触发的一个原因。原来pencilblue主题(控制管理员)不使用该服务来保存页面,而是直接进入DAO。 github.com/pencilblue/pencilblue/blob/master/plugins/pencilblue/…
标签: pencilblue