Created
August 2, 2013 04:03
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better yet, just call
fetch
with({remove: false, merge: false})
. For prepending instead of appending, passat: 0
with the other options. No monkey-patching necessary :)