我试图弄清楚当从回调调用 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/