Ember.js:this._super在回调函数中 [英] Ember.js: this._super in a callback function
问题描述
$ b当我在回调函数中调用Ember的对象方法时,我想要知道如何使用
this._super
$ b 我知道我可以在调用回调之前分配 var _super = this._super
,但我不喜欢。
我想让这个
对象在回调中包含正确的 _super
方法。 / p>
我的代码在这里: 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){
//异步回调
Ember.run ){
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未定义
// _super ()将工作
},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')
});
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.
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')
});
这篇关于Ember.js:this._super在回调函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!