javascript chainning从回调中返回 [英] javascript chainning return this from callback
问题描述
这里是剪切的
$ b $ b
create:function(currentView,data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper()。hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn(fast,function(){
return itsMe;
});
},
var l = list()。create(currentView); //需要返回,我可以使用链接
var l现在未定义,如果我使用fadeIn与回调...
如果我不使用fadeIn与回调它返回obj
任何人的想法为什么?
什么@Felix Kling说是正确的,你不返回任何东西。如果您要返回 itsMe
,您需要执行以下操作:
create :function(currentView,data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper()。hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn(fast);
return itsMe;
}
如果你想链接,这应该就足够了。
如果您想在淡出完成时获取对 itsMe
的引用,您需要传递自己的callback:
create:function(currentView,data,callback){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper()。hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn(fast,function(){
callback(itsMe);
});
}
list()。create(function){
console.log(fade out complete);
console.log (itsMe is,that);
});
如果你想有一个链接模式,它会执行链中的下一个函数完成后,您将需要传递一个引用 this
,但是一个对象可以排队命令,顺序实现每个命令。
i've tried to get a return this from a callback, but i always get undefined.
here is the snipped
create: function(currentView, data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast", function(){
return itsMe;
});
},
var l = list().create(currentView); //need teh return that i can use chaining
the var l is now undefined, if i use the fadeIn with a callback... if i dont use fadeIn with the callback it returns the obj
anyone an idea why?
What @Felix Kling say is correct, you are not returning anything. If you want to return itsMe
you will need to do:
create: function(currentView, data){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast");
return itsMe;
}
Which should suffice if you want chaining.
If you want to get a reference to itsMe
when the fadeout is finished, you will need to pass your own callback:
create: function(currentView, data, callback){
var itsMe = this;
this.thumbsWrapper = this.templates.wrapper().hide();
currentView.append(this.thumbsWrapper);
this.thumbsWrapper.fadeIn("fast", function(){
callback(itsMe);
});
}
list().create(function (that) {
console.log("fade out complete");
console.log("itsMe is", that);
});
And if you want to have a chaining pattern, which will execute the next function in the chain when fadeout is finished, you will need to pass not a reference to this
but an object which can queue up commands, implement each command sequentially.
这篇关于javascript chainning从回调中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!