Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        }
    }
});
like image 427
Nick Ragaz Avatar asked Oct 03 '22 02:10

Nick Ragaz


1 Answers

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.

like image 74
Luke Melia Avatar answered Oct 13 '22 09:10

Luke Melia