从Ember控制器上的事件处理程序调用`super`

Nic*_*gaz 5 javascript ember.js

最近,更新了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
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

Luk*_*lia 4

Ember 使您可以做您想做的事情。这是一个 JSFiddle,演示了它是如何工作的:

http://jsfiddle.net/HzjUG/1/

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);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

当 Ember 创建对象时,它会专门包装这些函数,以便它们可以使用 _super 。

如果您想分享更多的实现,我可以尝试帮助找出为什么您的代码的行为与 JSFiddle 演示的方式不同。