Permalink
Browse files

fixed edge case with empty responses

  • Loading branch information...
1 parent 8203776 commit ec25412d7f226be79ad869ceb0efceb899f6a97a @masylum committed Feb 13, 2012
Showing with 60 additions and 4 deletions.
  1. +1 −1 backbone.subset.js
  2. +59 −3 test/test.js
View
@@ -207,7 +207,7 @@
}
// model == collection
- if (ev === 'reset' && model !== this && model.any(this.sieve)) {
+ if (ev === 'reset' && model !== this) {
if (!collection.subset_reset) {
this._resetSubset(model.models, collection);
}
View
@@ -214,9 +214,9 @@ describe('Aggregated collections', function () {
assert.deepEqual(tasks.pluck('id'), [0, 1, 2]);
assert.deepEqual(archived_tasks.pluck('id'), [2, 1]);
assert.deepEqual(urgent_tasks.pluck('id'), [1, 0]);
- // assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c7', 'c8']);
- // assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c8', 'c7']);
- // assert.deepEqual(_.pluck(urgent_tasks.models, 'cid'), ['c7', 'c6']);
+ assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c7', 'c8']);
+ assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c8', 'c7']);
+ assert.deepEqual(_.pluck(urgent_tasks.models, 'cid'), ['c7', 'c6']);
happened = {
archived_tasks: 0
@@ -352,7 +352,63 @@ describe('Aggregated collections', function () {
assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c7', 'c8', 'c9', 'c10', 'c11', 'c12']);
assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c11', 'c10']);
assert.deepEqual(_.pluck(urgent_tasks.models, 'cid'), ['c12', 'c11', 'c7']);
+ });
+
+ it('proxies the `reset` event on empty responses', 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'), ['c13', 'c14', 'c15']);
+ assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c15', 'c14']);
+
+ happened = {
+ archived_tasks: 0
+ , tasks: 0
+ , tasks_add: 0
+ , archived_tasks_add: 0
+ , tasks_remove: 0
+ , archived_tasks_remove: 0
+ };
+
+ _.each([tasks, archived_tasks], function (coll) {
+ coll.unbind('add');
+ coll.unbind('reset');
+ coll.unbind('remove');
+ });
+ tasks.bind('add', inc('tasks_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'));
+ archived_tasks.bind('remove', inc('archived_tasks_remove'));
+
+ archived_tasks.reset([]);
+
+ assert.equal(happened.tasks_add, 0);
+ assert.equal(happened.archived_tasks_add, 0);
+ 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, 1, 2]);
+ assert.deepEqual(archived_tasks.pluck('id'), []);
+ assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c13', 'c14', 'c15']);
+ assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), []);
});
});

0 comments on commit ec25412

Please sign in to comment.