javascript - Ember.js:回调函数中的 this._super

标签 javascript ember.js callback super

我试图弄清楚当从回调调用 Ember 的对象方法时如何使用 this._super

我知道我可以在调用回调之前分配 var _super = this._super 但我不喜欢它。

我希望 this 对象在回调中包含正确的 _super 方法。

我的代码在这里: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();
  }
});

你知道有什么办法可以解决吗?


更新:

事实证明,它在 Ember 1.5.0 中已修复(@GJK:谢谢您的回答),而我使用的是 Ember 1.4.0。

最佳答案

extend 定义一个类

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

create 构建类的实例

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

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

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

或者避免重写 init

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

关于javascript - Ember.js:回调函数中的 this._super,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24144471/

相关文章:

javascript - 将 Sinon 与 Typescript 和接口(interface)结合使用

javascript - 如何使用鼠标在内容上滚动可滚动的 div?

ember.js - Ember : How do I find handlebars partial in the directory?

iOS8 Safari 图标字体在浏览器刷新之前不会加载

java - WebServiceTemplate - 拦截器和回调之间的区别?

JavaScript Jest 测试;使用回调函数修改数组中的元素

javascript - 从服务器 PHP 数组中提取 JSON 数据时出现问题

javascript - 如何将 RethinkDB 与 EmberJS 框架结合使用?

python - 我可以使用 ctypes 为可变参数 Python 函数创建原型(prototype),以便 DLL 可以将此函数作为回调调用吗?

javascript - 如何在导入语句中使用变量