Skip to content

Instantly share code, notes, and snippets.

@hshoff
Created February 21, 2012 07:51
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/1874970 to your computer and use it in GitHub Desktop.
Save hshoff/1874970 to your computer and use it in GitHub Desktop.
A nice way to initialize
class Component extends Backbone.View
initialize: ->
for func, args of @options
unless _.isArray(args)
@[func]?.call(@, args)
else
@[func]?.apply(@, args)
return null
render: =>
@
show: =>
@delegateEvents(@events)
@class('show')
@
class: (klass) =>
return @ unless klass?
@$el.addClass(klass)
@
# Then you can do this
component = new Component
'class': 'new-component admin'
'render': true
'show': true
# component.el = <div class='new-component admin'></div>
# for the compiled JavaScript:
http://coffeescript.org/#try:Backbone.Component extends Backbone.View%0A%0A initialize: ->%0A for func, args of @options%0A unless _.isArray(args)%0A @[func]?.call(@, args)%0A else%0A @[func]?.apply(@, args)%0A%0A render: =>%0A @$parent.append(@el)%0A @%0A%0A show: =>%0A @delegateEvents(@events)%0A @class('show')%0A @%0A %0A class: (klass) =>%0A return @ unless klass?%0A @$el.addClass(klass)%0A @%0A%0A%23 Then you can do this%0Acomponent = new Backbone.Component%0A 'class': 'new-component admin'%0A 'render': true%0A 'show': true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment