Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Backbone.BaseCollection = Backbone.Collection.extend({
- // Holds id of locally deleted items we ignore if we
- // 'freshen' the collection and changes haven't propagated yet.
- _localDeletedIds: [],
- // Take an array of raw objects
- // If the ID matches a model in the collection, set that model
- // If the ID is not found in the collection, add it
- // If a model in the collection is no longer available, remove it
- //
- // Keeps local changes, in case we've added things in the meantime.
- freshen: function(objects) {
- var model;
- // Mark all for removal, unless local only change
- this.each(function(m) {
- if (!m.isNew()) m._remove = true;
- });
- // Apply each object
- _(objects).each(function(attrs) {
- model = this.get(attrs.id);
- if (model) {
- model.set(attrs); // existing model
- delete model._remove
- } else {
- // add new models, accounting for local deletions
- var locallyDeleted = _.find(this._localDeletedIds,
- function(id){ return id == attrs.id });
- if (!locallyDeleted) this.add(attrs);
- }
- }, this);
- // Now check for any that are still marked for removal
- var toRemove = this.filter(function(m) {
- return m._remove;
- })
- _(toRemove).each(function(m) {
- this.remove(m);
- }, this);
- this.trigger('freshen', this);
- },
- remove: function(models, options) {
- models = _.isArray(models) ? models.slice() : [models];
- for (i = 0, l = models.length; i < l; i++) {
- if (models[i].id) this._localDeletedIds.push(models[i].id);
- }
- return Backbone.Collection.prototype.remove.call(this, models, options);
- }
- });
Add Comment
Please, Sign In to add comment