Skip to content

Instantly share code, notes, and snippets.

@takempf
Created October 23, 2012 00:42
Show Gist options
  • Select an option

  • Save takempf/3935927 to your computer and use it in GitHub Desktop.

Select an option

Save takempf/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