Skip to content

Instantly share code, notes, and snippets.

@halfbaked
Created April 28, 2012 05:43
Show Gist options
  • Save halfbaked/2516351 to your computer and use it in GitHub Desktop.
Save halfbaked/2516351 to your computer and use it in GitHub Desktop.
Auto-update collection changes in backbone
class sp.collections.DogCollection extends Backbone.Collection
initialize: ->
@fetch()
setInterval(@fetchChanges, 15*1000)
model: sp.models.DogModel
url: '/dogs'
changesUrl: ->
u = @url
if @length > 0 then u += "?lastUpdated="+@lastUpdated()
u
fetchChanges: =>
@fetch({add:true, merge:true, url: @changesUrl()})
lastUpdated: ->
lastModelUpdated = @max (model) ->
new Date(model.get('lastUpdated'))
lastModelUpdated.get('lastUpdated')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment