Skip to content

Instantly share code, notes, and snippets.

@flintinatux
Created October 29, 2014 17:26
Show Gist options
  • Save flintinatux/772455da61354ee2f406 to your computer and use it in GitHub Desktop.
Save flintinatux/772455da61354ee2f406 to your computer and use it in GitHub Desktop.
Backbone view layer
class CompositeView extends Backbone.View
initialize: (options) ->
super(options)
@children = _([])
renderChild: (child) ->
@children.push child
child.parent = this
child.render()
remove: ->
@trigger 'remove'
super()
@_removeChildren()
@_removeFromParent()
this
swapped: ->
@trigger 'swapped'
this
_removeChild: (child) ->
index = @children.indexOf child
@children.splice index, 1
_removeChildren: ->
_.each @children.clone(), (child) ->
child.remove()
_removeFromParent: ->
@parent._removeChild(this) if @parent
module.exports = CompositeView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment