Skip to content

Instantly share code, notes, and snippets.

@billyvg
Created June 14, 2012 19:20
Show Gist options
  • Save billyvg/2932337 to your computer and use it in GitHub Desktop.
Save billyvg/2932337 to your computer and use it in GitHub Desktop.
Backbone.js ListView + RowView
class ListView extends Backbone.View
constructor: (options) ->
super
@view = options.view if options.view?
@collection.on 'add', @addRow, @
@collection.on 'remove', @removeRow, @
@collection.on 'reset', @render, @
@
###
# Called when a model is added to the collection.
# Renders a new row in the table view.
###
addRow: (model) ->
view = new @view
model: model
@$el.append(view.render().el)
view.delegateEvents()
model.view = view
@
###
# Called when a model is removed from the collection.
# Removes the item from the table view.
###
removeRow: (model) ->
model.clear()
@
# Clears the table view of Assets and re-renders it.
render: ->
@$el.html('')
@collection.map (model) =>
@addRow model
@mindscratch
Copy link

what's the point in calling delegateEvents on the child view? (just trying to understand it's usage)

@billyvg
Copy link
Author

billyvg commented Jun 15, 2012

It's actually not needed, left it in there by mistake (old code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment