Ember.js: this._super in a callback function

2019-07-18 20:02发布

I'm trying to figure out how to use this._super when the Ember's object method is called from a callback.

I know that I could assign var _super = this._super before the callback is called but I don't like it.

I want to have the this object containing proper _super method inside the callback.

My code is here: http://emberjs.jsbin.com/hasehija/6/edit.

App.BaseMixin = Ember.Mixin.create({
  init: function() {
    console.log("base");
  }
});

App.Utils = Ember.Object.extend({
  callbackMethod: function(callback, ctx) {
    // asynchronous callback
    Ember.run(function() {
      callback.call(ctx);
    });
  }
});

App.MyObject = Ember.Object.extend(App.BaseMixin, {
  init: function() {
    console.log("MyObject");
    var _super = this._super;
    App.Utils.create().callbackMethod(function() {
      this._super(); // this._super is undefined here
      // _super() would work
    }, this);
  }
});

App.ApplicationController = Ember.Controller.extend({
  init: function() {
    new App.MyObject();
  }
});

Do you know any way to fix it?


UPDATE:

It turned out that it was fixed in Ember 1.5.0 (@GJK: thank you for the answer) and I was using Ember 1.4.0.

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-18 20:27

extend defines a class

App.Utils = Ember.Object.extend({
  callbackMethod: function(callback, ctx) {
    callback.call(ctx);
  }
});

create builds an instance of the class

App.Utils = Ember.Object.create({
  callbackMethod: function(callback, ctx) {
    callback.call(ctx);
  }
});

or

App.Utils.create().callbackMethod(function() {
  this._super(); 
}, this);

http://emberjs.jsbin.com/hasehija/7/edit

Or avoid overriding init

App.ApplicationController = Ember.Controller.extend({
  doSomething: function() {
    new App.MyObject();
  }.on('init')
});
查看更多
登录 后发表回答