var PlaylistView = Backbone.View.extend({
el: '#expanded-container',
initialize: function() {
this.bg = chrome.extension.getBackgroundPage();
this.$('.list-group').empty();
var realThis = this;
_.each(this.bg.Playlist.models, function (song) {
// append to playlist, rendering song template?
var songView = new SongView({ model: song });
console.log(realThis); // THIS is what I want
console.log(this) // this is NOT what I want
//this.$el.append(songView.render().el); // hence, this does NOT work
realThis.$el.append(songView.render().el); // and THIS works
});
}
});
In the above code, this
inside _.each()
function points at the global window
object because _.each()
is invoked by the window. However, I still want this
to point at PlaylistView
. I have faced many similar situations and I often defined a variable that stored the initial value of this, just like realThis
variable in the provided example. Is there any other conventional way to deal with this?
Note: I am following this book to learn Backbone, and it shows the following code as example.
var ListView = Backbone.View.extend({
render: function(){
// Assume our model exposes the items we will
// display in our list
var items = this.model.get('items');
// Loop through each of our items using the Underscore
// _.each iterator
_.each(items, function(item){
// Create a new instance of the ItemView, passing
// it a specific model item
var itemView = new ItemView({ model: item });
// The itemView's DOM element is appended after it
// has been rendered. Here, the 'return this' is helpful
// as the itemView renders its model. Later, we ask for
// its output ("el")
this.$el.append( itemView.render().el ); // <--- *THIS IS WRONG?
}, this);
}
});
In this case, wouldn't this
inside _.each
loop point at the wrong object, just like in my code? Is this an error in the book or am I misunderstanding something? Thank you!
Reference: Learning this keyword