Skip to content

Instantly share code, notes, and snippets.

@Fauntleroy
Created October 23, 2012 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fauntleroy/3935927 to your computer and use it in GitHub Desktop.
Save Fauntleroy/3935927 to your computer and use it in GitHub Desktop.
bindEvents pattern
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