Skip to content

Commit 2a38718

Browse files
committed
Build an Index using its collection's class.
1 parent 80ac2b1 commit 2a38718

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/indexer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020

2121
Model.Indexer.prototype.get = function(key) {
22-
return this.index[key] || (this.index[key] = new Model.Collection())
22+
return this.index[key] || (this.index[key] = new this.collection.constructor())
2323
}
2424

2525
Model.Indexer.prototype.remove = function(model) {

test/tests/indexer_test.js

+18
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,21 @@ test("with a custom #toKey", function() {
6363
ok(index.get("2012-2-3").last() === story3)
6464
ok(index.get("2012-2-14").first() === story2)
6565
})
66+
67+
test("creates collections of the same class as its source", function() {
68+
var Story = Model("story", function(_, _, collection) {
69+
collection.foo = function() {
70+
return "foo"
71+
}
72+
})
73+
74+
var story1 = new Story({ project_id: 1 })
75+
var story2 = new Story({ project_id: 2 })
76+
var story3 = new Story({ project_id: 3 })
77+
78+
Story.collection.push(story1, story2, story3)
79+
80+
var index = new Model.Indexer(Story.collection, "project_id")
81+
82+
equal(index.get(1).foo(), "foo")
83+
})

0 commit comments

Comments
 (0)