Skip to content

Instantly share code, notes, and snippets.

@misuba
Created June 5, 2012 23:58
Show Gist options
  • Save misuba/2878958 to your computer and use it in GitHub Desktop.
Save misuba/2878958 to your computer and use it in GitHub Desktop.
some Backbone.View extensions I'm pretty sure I will never not want
ViewPlus = Backbone.View.extend
initialize: (opts) ->
@init opts if @init?
if opts.partials?
Handlebars.registerPartial vuname, vu for vu, vuname in opts.partials
@_compiledTemplate = Handlebars.compile @template
render: ->
@beforeRender() if @beforeRender?
theElm = @_compiledTemplate @model.toJSON()
@$el.append theElm
g: (what) -> @model.get what
s: (pairs) -> @model.set pairs
ss: (pairs) ->
@s pairs
@save
CViewPlus = ViewPlus.extend
init: (opts) ->
return false unless opts.view?
@collection.on 'add', @addOne, @
@collection.on 'reset', @addAll, @
addOne: (one) ->
itemView = new @options.view
model: one
@$el.append itemView.render().el
addAll: ->
@$el.empty()
@collection.forEach @addOne, @
render: ->
@addAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment