问题描述
在 ember
路由中有多个 beforeModel
钩子有什么问题吗?
Is there anything wrong with having multiple beforeModel
hooks in an ember
route?
例如,如果我有一个 mixin
,它添加了一个 beforeModel
钩子,然后在路由中添加了另一个 beforeModel
到".
For example, if I have a mixin
that adds a beforeModel
hook, and then another beforeModel
in the route I'm "mixing in to".
推荐答案
不,如果你在路由和 mixin 上定义了它,路由将胜出.在下面的示例中,只会调用 bar
.
No, if you have it defined on the route and mixin, the route will win out. In the example below, only bar
will be called.
App.Foo = Ember.Mixin.create({
beforeModel: function(transition, queryParams){
console.log('foo');
}
})
App.IndexRoute = Ember.Route.extend(App.Foo,{
beforeModel: function(transition, queryParams){
console.log('bar');
},
model: function() {
return ['red', 'yellow', 'blue'];
}
});
http://emberjs.jsbin.com/runuowe/1/edit
如果你愿意,你可以从扩展类调用 this._super(param1, param2...) 来调用基方法.
If you want though, you can call this._super(param1, param2...) from the extended class to call the base method.
App.IndexRoute = Ember.Route.extend(App.Foo,{
beforeModel: function(transition, queryParams){
this._super(transition, queryParams);
console.log('bar');
},
model: function() {
return ['red', 'yellow', 'blue'];
}
});
http://emberjs.jsbin.com/runuowe/4/edit
这篇关于Ember.js - 多个 beforeModel 钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!