Permalink
Browse files

propagate reset correctly

  • Loading branch information...
1 parent da5f253 commit 200d1b175118c2b9040a4832cbee84a0c2955ac4 @masylum committed Feb 6, 2012
Showing with 18 additions and 13 deletions.
  1. +13 −8 backbone.subset.js
  2. +5 −5 test/test.js
View
@@ -48,21 +48,26 @@
* @return {Object} collection
*/
Subset.reset = function (models, options) {
+ var parent_models = _.clone(this.parent().models)
+ , ids = this.pluck('id');
+
models = models || [];
models = _.isArray(models) ? models : [models];
options = options || {};
// delete parent reseted models
- this.each(function (model) {
- this.parent()._remove(model, _.extend(options, {noproxy: true}));
+ parent_models = _.reject(parent_models, function (model) {
+ return ids.indexOf(model.id) !== -1;
}, this);
// insert parent reseted models
_.each(models, function (model) {
- this.parent()._add(model, _.extend(options, {noproxy: true}));
+ parent_models.push(model);
}, this);
- return this._resetSubset(models, options);
+ this.parent().reset(parent_models, options);
+
+ return this;
};
/**
@@ -80,9 +85,7 @@
this._reset();
this.parent().each(function (model) {
- if (this.sieve(model)) {
- this._addToSubset(model, {silent: true});
- }
+ this._addToSubset(model, {silent: true});
}, this);
if (!options.silent) {
@@ -111,7 +114,9 @@
* @return {Object} model
*/
Subset._addToSubset = function (model, options) {
- return Backbone.Collection.prototype._add.call(this, model, options);
+ if (this.sieve(model)) {
+ return Backbone.Collection.prototype._add.call(this, model, options);
+ }
}
/**
View
@@ -209,7 +209,7 @@ describe('Aggregated collections', function () {
archived_tasks.unbind('remove');
tasks.bind('add', inc('tasks_add'));
- archived_tasks.bind('add', inc('threads_add'));
+ archived_tasks.bind('add', inc('archived_tasks_add'));
tasks.bind('reset', inc('tasks'));
archived_tasks.bind('reset', inc('archived_tasks'));
tasks.bind('remove', inc('tasks_remove'));
@@ -229,11 +229,11 @@ describe('Aggregated collections', function () {
, order: 6
}]);
- assert.equal(happened.tasks, 0);
- assert.equal(happened.archived_tasks, 1);
- assert.equal(happened.tasks_add, 3);
+ assert.equal(happened.tasks_add, 0);
assert.equal(happened.archived_tasks_add, 0);
- assert.equal(happened.tasks_remove, 2);
+ assert.equal(happened.tasks, 1);
+ assert.equal(happened.archived_tasks, 1);
+ assert.equal(happened.tasks_remove, 0);
assert.equal(happened.archived_tasks_remove, 0);
assert.deepEqual(tasks.pluck('id'), [0, 4, 5, 6]);

0 comments on commit 200d1b1

Please sign in to comment.