Skip to content

Instantly share code, notes, and snippets.

@imehr
Created February 16, 2012 11:34
Show Gist options
  • Save imehr/1844243 to your computer and use it in GitHub Desktop.
Save imehr/1844243 to your computer and use it in GitHub Desktop.
_.bindAll()
/*
_.bindAll() ensures that all the functions you indicate are always invoked in the specified context. This is especially useful for event callbacks, as their context is always changing.
*/
Account = Backbone.Model.extend({
initialize: function() {
_.bindAll(this, 'removeElement');
},
removeElement: function() {
$(this.view.el).remove();
},
clear: function() {
this.destroy({success: this.removeElement});
}
})
var TodoView = Backbone.View.extend({ initialize: function() {
_.bindAll(this, 'render', 'close');
this.model.bind('change', this.render); },
close: function(){ /* ... */ }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment