-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.js
45 lines (42 loc) · 1.11 KB
/
collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
namedInstances = {};
nullInstances = [];
CollectionExtensions.addExtension(function(name, options) {
var Collection = this;
if (options && typeof(options.ref) == 'string') var name = options.ref;
this._ref = name;
if (name) {
if (!(name in namedInstances)) namedInstances[name] = [];
namedInstances[name].push(this);
}
else {
nullInstances.push(this);
}
this.helpers({
Collection: function() {
return Collection;
}
});
});
// () => [Mongo.Collection]
Shuttler.collections = function() {
var results = [];
lodash.each(namedInstances, function(instances) {
lodash.each(instances, function(instance) {
results.push(instance);
});
});
lodash.each(nullInstances, function(instance) {
results.push(instance);
});
return results;
};
// (condition: String|Function) => Mongo.Collection|undefined
Shuttler.collection = function(condition) {
if (typeof(condition) == 'string') {
if (condition in namedInstances && namedInstances[condition].length) {
return namedInstances[condition][0];
}
} else if (typeof(condition) == 'function') {
return lodash.find(Shuttler.collections(), condition);
}
};