Permalink
Browse files

Add 'no_reset' option to NOT do a reset on construction but rather in…

…itialize with existing parent models.

i.e. it will ignore any models passed as args to the constructor and
instead do an internal silent reset.
  • Loading branch information...
1 parent ec25412 commit 36403260018991b7cdd26233f757582bfe7d0f4c @saimonmoore saimonmoore committed Feb 15, 2012
Showing with 62 additions and 3 deletions.
  1. +9 −3 backbone.subset.js
  2. +53 −0 test/test.js
View
@@ -35,11 +35,17 @@
this.beforeInitialize.apply(this, arguments);
}
- this._reset();
+ if (!options.no_reset) {
+ this._reset();
- if (models) {
- this.reset(models, {silent: true});
+ if (models) {
+ this.reset(models, {silent: true});
+ }
}
+ else {
+ this._resetSubset(this.parent().models, {silent: true});
+ }
+
this.initialize.apply(this, arguments);
};
View
@@ -410,6 +410,59 @@ describe('Aggregated collections', function () {
assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c13', 'c14', 'c15']);
assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), []);
});
+
+ it('can be initialized without resetting and does an internal reset', function () {
+ happened = {archived_tasks: 0, tasks: 0};
+
+ tasks.unbind('reset');
+ archived_tasks.unbind('reset');
+ tasks.bind('reset', inc('tasks'));
+ archived_tasks.bind('reset', inc('archived_tasks'));
+
+ tasks.reset([ {id: 0, archived: 0, order: 0}
+ , {id: 1, archived: 1, order: 1}
+ , {id: 2, archived: 1, order: 2}]);
+
+ assert.equal(happened.tasks, 1);
+ assert.equal(happened.archived_tasks, 1);
+
+ assert.deepEqual(tasks.pluck('id'), [0, 1, 2]);
+ assert.deepEqual(archived_tasks.pluck('id'), [2, 1]);
+ assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c16', 'c17', 'c18']);
+ assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c18', 'c17']);
+
+ happened = {
+ archived_tasks: 0
+ , tasks: 0
+ , tasks_add: 0
+ , archived_tasks_add: 0
+ , tasks_remove: 0
+ , archived_tasks_remove: 0
+ };
+
+ _.each([tasks], function (coll) {
+ coll.unbind('add');
+ coll.unbind('reset');
+ coll.unbind('remove');
+ });
+
+ tasks.bind('add', inc('tasks_add'));
+ tasks.bind('reset', inc('tasks'));
+ tasks.bind('remove', inc('tasks_remove'));
+
+ assert.deepEqual(tasks.pluck('id'), [0, 1, 2]);
+
+ archived_tasks = new Collections.ArchivedTasks([], {no_reset: true});
+
+ assert.equal(happened.tasks_add, 0);
+ assert.equal(happened.tasks, 0);
+ assert.equal(happened.tasks_remove, 0);
+
+ assert.deepEqual(tasks.pluck('id'), [0, 1, 2]);
+ assert.deepEqual(archived_tasks.pluck('id'), [2, 1]);
+ assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c16', 'c17', 'c18']);
+ assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c18', 'c17']);
+ });
});
describe('Live updating subset membership', function () {

0 comments on commit 3640326

Please sign in to comment.