Created
June 5, 2012 23:58
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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