javascript - self.el vs this.el -
i'm following backbone.js tutorial , came across 2 functions initialize() , render(). initialize() used $(self.el).append() when appending html while render() used $(this.el).append(). confused difference, appreciate explaination, thanks!
js code
// views window.winelistview = backbone.view.extend({ tagname:'ul', initialize:function () { this.model.bind("reset", this.render, this); var self = this; this.model.bind("add", function (wine) { $(self.el).append(new winelistitemview({model:wine}).render().el); }); }, render:function (eventname) { _.each(this.model.models, function (wine) { $(this.el).append(new winelistitemview({model:wine}).render().el); }, this); return this; } });
the first uses self keep reference this when scope changes when event fires. inside anonymous function (for event handler), this refer element fired event rather backbone controller.
the reference isn't needed in second case.
Comments
Post a Comment