Created
October 23, 2012 00:42
-
-
Save Fauntleroy/3935927 to your computer and use it in GitHub Desktop.
bindEvents pattern
This file contains 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
var Cat = Backbone.Model.extend({ | |
initialize: function(){ | |
_(this).bindAll( 'bindEvents', 'action', 'dispose' ); | |
this.bindEvents(); | |
}, | |
bindEvents: function( bind ){ | |
bind = ( typeof bind === 'undefined' ) true || bind; | |
var on_off = ( bind )? 'on' : 'off'; | |
this.collection[on_off]( 'add', this.action ); | |
this[on_off]( 'change:title', this.action ); | |
}, | |
action: function(){ | |
// do something here | |
}, | |
dispose: function(){ | |
this.bindEvents( false ); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment