Skip to content Skip to sidebar Skip to footer

Javascript Chainning Return This From Callback

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

Solution 1:

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.

Solution 2:

You need to return the object in the create() function, it is currently not returning anything:

create: function(currentView, data){
    var itsMe = this; 
    this.thumbsWrapper = this.templates.wrapper().hide();
    currentView.append(this.thumbsWrapper);
    this.thumbsWrapper.fadeIn("fast", function(){
        return itsMe;  //<--- this isn't going anywhere because you don't capture it                                               
    });
    return itsMe; //<------ return the object
},

Post a Comment for "Javascript Chainning Return This From Callback"