Skip to content

Instantly share code, notes, and snippets.

@therabidbanana
Created August 9, 2012 16:26
Show Gist options
  • Save therabidbanana/3305643 to your computer and use it in GitHub Desktop.
Save therabidbanana/3305643 to your computer and use it in GitHub Desktop.
#= require ./view
class Dtime.Backbone.CompositeView extends Dtime.Backbone.View
leave: ->
this.unbindFromAll()
this.remove()
this._leaveChildren()
this._removeFromParent()
renderChild: (view)->
view.parent = this
view.render()
this.children ||= _([])
this.children.push(view)
appendChild: (view)->
this.renderChild(view)
$(@el).append(view.el)
prependChild: (view)->
this.renderChild(view)
$(@el).prepend(view.el)
renderChildInto: (view, container)->
this.renderChild(view)
$(container).empty().append(view.el)
appendChildInto: (view, container)->
this.renderChild(view)
@$(container).append(view.el)
prependChildInto: (view, container)->
this.renderChild(view)
@$(container).prepend(view.el)
_leaveChildren: ->
this.children?.chain().clone().each (view)->
view.leave?()
_removeFromParent: ()->
@parent?._removeChild(this)
_removeChild: (view)->
index = this.children.indexOf(view)
this.children.splice(index, 1)
#= require ./view
class Dtime.Backbone.Observer
bindToModel: (event, callback) ->
source = @model
@bindTo(source, event, callback)
bindTo: (source, event, callback) ->
source.bind(event, callback, this)
this.bindings = this.bindings || []
this.bindings.push({ source: source, event: event, callback: callback })
unbindFromAll: () ->
_.each(this.bindings, (binding) ->
binding.source.unbind(binding.event, binding.callback);
)
this.bindings = []
_.extend(Dtime.Backbone.View::, Dtime.Backbone.Observer::)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment