我正在尝试操作 Hook http://docs.strongloop.com/display/public/LB/Operation+hooks
这是我在应用程序代码中所做的:
mymodel.observe('before save', doSomething);
//after some time elapses or based on an event we want to change the behaviour
mymodel.observe('before save', doSomethingElse);
var doSomething = function (ctx, next) {
//do something
next();
};
var doSomethingElse = function (ctx, next) {
//do something else
next();
};
当我测试这段代码时,我发现总是执行 doSomething,这让我想知道观察者函数是否每个模型只能注册一次,还是一个错误?
如果是按照设计,你能告诉它背后的原因吗?
最佳答案
免责声明:我是 LoopBack 的核心开发者,也是 Operation hooks 的作者。
after some time elapses or based on an event we want to change the behaviour
操作钩子(Hook)还不支持注销处理函数。每次调用
observe()
将处理程序添加到触发钩子(Hook)时调用的方法列表。When I test this code I find that always doSomething is executed which makes me wonder if the observer function can be registered only once per model or is it a bug?
您可以注册多个观察者。一旦您注册了
doSomething
,它会一直被调用。注册时doSomethingElse
,它也会被调用,在 doSomething
之后通过 next()
返回.
关于loopbackjs - 每个模型只能注册一次 loopback.io 操作 Hook 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28895097/