简体   繁体   中英

Calling `super` from an event handler on an Ember controller

Recently, Ember.js was updated so that action event handlers are defined in an actions object on routes/controllers/views. As a result, event handlers are no longer normal methods on the prototype.

If you subclass a (for example) controller using extend , is it still possible to override and then call the superclass's handler?

Just calling _super doesn't work:

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 makes it possible to do what you are trying to do. Here is a JSFiddle that demonstrates how this works:

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);
    }
  }
});

When Ember is creating an object, it specially wraps these functions so that they have _super available to them.

If you'd like to share more of your implementation, I can try to help figure out why your code is not behaving the way the JSFiddle demonstration is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM