[英]Calling `super` from an event handler on an Ember controller
最近, 更新了Ember.js,以便在路径/控制器/视图上的actions
对象中定义操作事件处理程序 。 因此,事件处理程序不再是原型上的常规方法。
如果使用extend
继承(例如)控制器,是否仍然可以覆盖然后调用超类的处理程序?
只是调用_super
不起作用:
FormController = Em.ObjectController.extend({
actions: {
submit: function() { this.get('model').save(); }
}
});
SpecialFormController = FormController.extend({
actions: {
submit: function() {
this.set('special', true);
this._super(); // doesn't work
}
}
});
Ember让你可以做你想做的事。 这是一个JSFiddle,演示了它是如何工作的:
App.BaseController = Em.ArrayController.extend({
actions: {
nameAlert: function(person){
window.alert('alert from BaseController: ' + person.lastName + ', ' + person.firstName);
}
}
});
App.IndexController = App.BaseController.extend({
actions: {
nameAlert: function(person){
this._super(person);
window.alert('alert from IndexController: ' + person.lastName + ', ' + person.firstName);
}
}
});
当Ember创建一个对象时,它会专门包装这些函数,以便它们可以使用_super。
如果您想分享更多的实现,我可以尝试帮助弄清楚为什么您的代码不像JSFiddle演示那样表现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.