Created
February 16, 2012 11:34
-
-
Save imehr/1844243 to your computer and use it in GitHub Desktop.
_.bindAll()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
_.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