Skip to content

Instantly share code, notes, and snippets.

@hshoff
Created February 21, 2012 07:35
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 hshoff/1874862 to your computer and use it in GitHub Desktop.
Save hshoff/1874862 to your computer and use it in GitHub Desktop.
Nice way to initialize Backbone.Views
Backbone.Component extends Backbone.View
initialize: ->
for func, args of @options
unless _.isArray(args)
@[func]?.call(@, args)
else
@[func]?.apply(@, args)
render: =>
@$parent.append(@el)
@
show: =>
@delegateEvents(@events)
@class('show')
@
class: (klass) =>
@$el.addClass(klass)
@
# Then you can do this
component = new Backbone.Component
'class': "new-component admin"
'render': true
'show': true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment