Skip to content

Instantly share code, notes, and snippets.

@jstrimpel
Created August 2, 2013 04:03
Show Gist options
  • Save jstrimpel/6137413 to your computer and use it in GitHub Desktop.
Save jstrimpel/6137413 to your computer and use it in GitHub Desktop.
What is the best method for handling polling activity feeds or infinite scroll in backbone collection views? I tied this into a backbone marionette collection view with an infinite scroll library. Is there a better way? Am I missing an easier way to handle these cases using a backbone collection?
var fetch = Backbone.Collection.prototype.fetch;
var reset = Backbone.Collection.prototype.reset;
Backbone.Collection.prototype.fetch = function (options) {
if (options.unshift || options.push) { // force a reset
options.reset = true;
}
return fetch.call(this, options);
};
Backbone.Collection.prototype.reset = function (models, options) {
if (!options.unshift && !options.push) {
reset.call(this, models, options);
return this;
}
var action = options.push ? 'push' : 'unshift',
method = action === 'push' ? 'add' : 'unshift';
this[method](models, _.extend({ silent: false }, options));
if (!options.silent) {
this.trigger(action, models);
}
return this;
};
@akre54
Copy link

akre54 commented Aug 3, 2013

Better yet, just call fetch with ({remove: false, merge: false}). For prepending instead of appending, pass at: 0 with the other options. No monkey-patching necessary :)

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