Ember.js: this._super in a callback function

时光怂恿深爱的人放手 提交于 2019-12-23 23:04:22

问题


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:


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


来源:https://stackoverflow.com/questions/24144471/ember-js-this-super-in-a-callback-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!