Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Ensure we respect existing comparator on subset

  • Loading branch information...
commit d858edda8038aac04f8e71740c35232733c8bc25 1 parent ed282a9
Saimon Moore authored December 01, 2011
2  backbone.subset.js
@@ -24,7 +24,7 @@
24 24
     options = options || {};
25 25
 
26 26
     this.model = this.parent().model;
27  
-    this.comparator = options.comparator || this.parent().comparator;
  27
+    this.comparator = this.comparator || options.comparator || this.parent().comparator;
28 28
 
29 29
     _.bindAll(this, '_onModelEvent', '_unbindModelEvents', '_proxyEvents');
30 30
 
47  test/test.js
@@ -26,7 +26,13 @@ Models.Task = Backbone.Model.extend({
26 26
   }
27 27
 });
28 28
 
29  
-Collections.Tasks = Backbone.Collection.extend({model: Models.Task});
  29
+Collections.Tasks = Backbone.Collection.extend({
  30
+  model: Models.Task
  31
+, comparator: function (m) {
  32
+    return m.get('order');
  33
+  }
  34
+});
  35
+
30 36
 Collections.ArchivedTasks = Backbone.Subset.extend({
31 37
   parent: function () {
32 38
     return tasks;
@@ -34,6 +40,9 @@ Collections.ArchivedTasks = Backbone.Subset.extend({
34 40
 , sieve: function (task) {
35 41
     return task.isArchived();
36 42
   }
  43
+, comparator: function (m) {
  44
+    return -m.get('order');
  45
+  }
37 46
 });
38 47
 
39 48
 tasks = new Collections.Tasks();
@@ -47,7 +56,7 @@ describe('Subset', function () {
47 56
     archived_tasks.bind('add', inc('archived_tasks'));
48 57
 
49 58
     for (var i = 0; i < 4; i++) {
50  
-      archived_tasks.add({id: i, archived: i % 2});
  59
+      archived_tasks.add({id: i, archived: i % 2, order: i});
51 60
     }
52 61
 
53 62
     assert.equal(happened.tasks, 4);
@@ -59,12 +68,12 @@ describe('Subset', function () {
59 68
 
60 69
   it('contains corrects cids', function () {
61 70
     assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c0', 'c1', 'c2', 'c3']);
62  
-    assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c1', 'c3']);
  71
+    assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c3', 'c1']);
63 72
   });
64 73
 
65 74
   it('contains corrects ids', function () {
66 75
     assert.deepEqual(tasks.pluck('id'), [0, 1, 2, 3]);
67  
-    assert.deepEqual(archived_tasks.pluck('id'), [1, 3]);
  76
+    assert.deepEqual(archived_tasks.pluck('id'), [3,1]);
68 77
   });
69 78
 
70 79
   it('has a `get` function that behaves like the `Collection` one + bubbling', function () {
@@ -96,14 +105,14 @@ describe('Aggregated collections', function () {
96 105
     tasks.bind('add', inc('tasks'));
97 106
     archived_tasks.bind('add', inc('archived_tasks'));
98 107
 
99  
-    tasks.add([{id: 0, archived: 0}, {id: 1, archived: 1}]);
  108
+    tasks.add([{id: 0, archived: 0, order: 0}, {id: 1, archived: 1, order: 1}]);
100 109
 
101 110
     assert.equal(happened.tasks, 2);
102 111
     assert.equal(happened.archived_tasks, 1);
103 112
 
104  
-    assert.deepEqual(tasks.pluck('id'), [2, 3, 0, 1]);
  113
+    assert.deepEqual(tasks.pluck('id'), [0, 1, 2, 3]);
105 114
     assert.deepEqual(archived_tasks.pluck('id'), [3, 1]);
106  
-    assert.deepEqual(_.pluck(tasks.models, 'cid'), ["c2","c3","c4","c5"]);
  115
+    assert.deepEqual(_.pluck(tasks.models, 'cid'), ["c4", "c5", "c2", "c3"]);
107 116
     assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c3', 'c5']);
108 117
   });
109 118
 
@@ -153,15 +162,15 @@ describe('Aggregated collections', function () {
153 162
     tasks.bind('reset', inc('tasks'));
154 163
     archived_tasks.bind('reset', inc('archived_tasks'));
155 164
 
156  
-    tasks.reset([{id: 0, archived: 0}, {id: 1, archived: 1}]);
  165
+    tasks.reset([{id: 0, archived: 0, order: 0}, {id: 1, archived: 1, order: 1}, {id: 2, archived: 1, order: 2}]);
157 166
 
158 167
     assert.equal(happened.tasks, 1);
159 168
     assert.equal(happened.archived_tasks, 1);
160 169
 
161  
-    assert.deepEqual(tasks.pluck('id'), [0, 1]);
162  
-    assert.deepEqual(archived_tasks.pluck('id'), [1]);
163  
-    assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c7']);
164  
-    assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c7']);
  170
+    assert.deepEqual(tasks.pluck('id'), [0, 1, 2]);
  171
+    assert.deepEqual(archived_tasks.pluck('id'), [2,1]);
  172
+    assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c7', 'c8']);
  173
+    assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c8','c7']);
165 174
 
166 175
     happened = {archived_tasks: 0, tasks: 0, tasks_add: 0, archived_tasks_add: 0, tasks_remove: 0, archived_tasks_remove: 0};
167 176
     tasks.unbind('add');
@@ -178,18 +187,18 @@ describe('Aggregated collections', function () {
178 187
     tasks.bind('remove', inc('tasks_remove'));
179 188
     archived_tasks.bind('remove', inc('archived_tasks_remove'));
180 189
 
181  
-    archived_tasks.reset([{id: 4, archived: 0}, {id: 5, archived: 1}]);
  190
+    archived_tasks.reset([{id: 4, archived: 0, order: 4}, {id: 5, archived: 1, order: 5}, {id: 6, archived: 1, order: 6}]);
182 191
 
183 192
     assert.equal(happened.tasks, 0);
184 193
     assert.equal(happened.archived_tasks, 1);
185  
-    assert.equal(happened.tasks_add, 2);
  194
+    assert.equal(happened.tasks_add, 3);
186 195
     assert.equal(happened.archived_tasks_add, 0);
187  
-    assert.equal(happened.tasks_remove, 1);
  196
+    assert.equal(happened.tasks_remove, 2);
188 197
     assert.equal(happened.archived_tasks_remove, 0);
189 198
 
190  
-    assert.deepEqual(tasks.pluck('id'), [0, 4, 5]);
191  
-    assert.deepEqual(archived_tasks.pluck('id'), [5]);
192  
-    assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c8', 'c9']);
193  
-    assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c9']);
  199
+    assert.deepEqual(tasks.pluck('id'), [0, 4, 5, 6]);
  200
+    assert.deepEqual(archived_tasks.pluck('id'), [6, 5]);
  201
+    assert.deepEqual(_.pluck(tasks.models, 'cid'), ['c6', 'c9', 'c10', 'c11']);
  202
+    assert.deepEqual(_.pluck(archived_tasks.models, 'cid'), ['c11', 'c10']);
194 203
   });
195 204
 });

0 notes on commit d858edd

Please sign in to comment.
Something went wrong with that request. Please try again.