Skip to content

Instantly share code, notes, and snippets.

@sulf
Created July 14, 2011 07:02
Show Gist options
  • Save sulf/1082046 to your computer and use it in GitHub Desktop.
Save sulf/1082046 to your computer and use it in GitHub Desktop.
Coffee Script Backbone.js AutoBind
AutoBind =
auto_bind: ->
funcs = _.functions(@constructor.prototype)
protoFuncs = ['auto_bind', 'constructor'].concat(
_.functions(Backbone.Collection.prototype),
_.functions(Backbone.Model.prototype),
_.functions(Backbone.View.prototype))
_.each funcs, (f) =>
if f.charAt(0) != '_' && _.indexOf(protoFuncs, f) == -1
@[f] = _.bind(@[f], @)
_.extend Backbone.Collection.prototype, AutoBind
_.extend Backbone.Model.prototype, AutoBind
_.extend Backbone.View.prototype, AutoBind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment