回环:如何添加模型的afterRemote到另一种模式
问题描述:
我有通知模型看起来像这样回环:如何添加模型的afterRemote到另一种模式
"use strict";
module.exports = function(Notification) {
};
我有另一种模式是邮政:
"use strict";
module.exports = function(Post) {
Post.prototype.postLike = function(options, cb) {
this.likes.add(options.accessToken.userId);
cb(null, "sucess");
};
Post.remoteMethod("postLike", {
isStatic: false,
accepts: [{ arg: "options", type: "object", http: "optionsFromRequest" }],
returns: { arg: "name", type: "string" },
http: { path: "/like", verb: "post" }
});
}
我要的是在通知模型里面添加PostRemote方法?
是否有可能在回送?
它不应该是这样的:
"use strict";
module.exports = function(Notification) {
var app = require("../../server/server.js");
var post = app.models.Post;
post.afterRemote('prototype.postLike', function(context, like, next) {
console.log('Notification after save for Like comment');
});
};
但是,这是行不通的。
注:我能做到这一点Post模型本身,但我想补充我所有的通知逻辑的通知模型简化和未来的定制。
答
您可以使用活动来完成。
回环可发射started
事件,当它启动的所有启动脚本加载here
和Notification
模型后这样做:
"use strict";
module.exports = function(Notification) {
var app = require("../../server/server.js");
app.on('started', function(){
var post = app.models.Post;
post.afterRemote('prototype.postLike', function(context, like, next) {
console.log('Notification after save for Like comment');
});
});
};
或者创建启动脚本,并发出像“allModelsLoaded自定义事件”。所以请确保引导脚本是最后一个要运行的脚本。引导脚本默认按字母顺序运行。因此,制作z.js
并在那里发出该自定义事件,然后在Notification
模型中听该事件。
答
环回启动过程首先加载模型,然后在所有模型加载后调用启动脚本。如果您的目标是跨模型合并事务,则最好在引导脚本中执行此操作,而不是在model.js文件中执行此操作。
我会检查一下。有没有什么办法可以在没有启动脚本的情况下在模型中执行它?或者像我应该在我的所有模型加载后检查它。 –