Skip to content

Instantly share code, notes, and snippets.

@dogenpunk
Created August 7, 2012 12:17
Show Gist options
  • Select an option

  • Save dogenpunk/3284853 to your computer and use it in GitHub Desktop.

Select an option

Save dogenpunk/3284853 to your computer and use it in GitHub Desktop.
Backbone.js StreamingCollection
var StreamCollection = Backbone.Collection.extend({
stream: function(options) {
this.unstream();
var _update = _.bind(function() {
this.fetch(options);
this._intervalFetch = window.setTimeout(_update, options.interval || 1000);
}, this);
_update();
},
unstream: function() {
window.clearTimeout(this._intervalFetch);
delete this._intervalFetch;
},
isStreaming: function() {
return _.isUndefined(this._intervalFetch);
}
});
// someCollection.stream({interval: 2000, add: true});
// see http://weblog.bocoup.com/backbone-live-collections for other notes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment