I am using backbone and doing the following:
var self = this;
$.when(commentCollections.fetch()).done(function(comments){
self.comments = comments;
console.log(self.comments);
}).fail(function(){
console.log('Could not fetch comments.');
});
console.log(this.comments);
As you can see I am trying to keep the reference of "this" in tack. self.comments
refers to a "class level variable" set to []
.
When I run this I get:
[]
Object {comments: Array[6]}
in the console. the []
refers to this.comments
and the the Object ...
refers to self.comments
One might say, well do everything inside the .done()
but I need, for other reason, this, what I am trying to do, concept to work.
Why is the scope of "this" not being preserved?