-
Notifications
You must be signed in to change notification settings - Fork 17
/
Collection.js
110 lines (87 loc) · 2.56 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Collection
*/
(function(define) {
define(function(require) {
var Base, resolver, eventTypes, simpleStrategy;
Base = require('./hub/Base');
resolver = require('./collectionAdapterResolver');
simpleStrategy = require('./network/strategy/default');
eventTypes = extend(Base.prototype.eventTypes, {
// collection item events. most of these come with data. devs can
// decide to use these events for their own purposes or send
// different data than described here, the following list outlines
// the intended behavior.
add: 1, // data == item added
remove: 1, // data == item removed
target: 1, // data == item targeted TODO: rename this to "point"?
// multi-item events
select: 1, // select an item (data == item)
unselect: 1, // deselect an item (data == item)
// batch events
collect: 1, // start of batch mode (until abort or submit) (data = batch purpose)
deliver: 1 // collected items (data = batch purpose with collected items array as property)
});
function Collection(options) {
Base.call(this, options);
if(!options) {
options = {};
}
this.strategy = options.strategy;
if (!this.strategy) this.strategy = simpleStrategy(options.strategyOptions);
}
Collection.prototype = Object.create(Base.prototype, {
eventTypes: { value: eventTypes },
resolver: { value: resolver },
forEach: {
value: function forEach(lambda) {
var provider = this.getProvider();
return provider && provider.forEach(lambda);
}
},
findItem: {
value: function (anything) {
var info = this._findItemFor(anything);
return info && info.item;
}
},
findNode: {
value: function (anything) {
var info = this._findNodeFor(anything);
return info && info.node;
}
},
getProvider: {
value: function () {
var a, i = this.adapters.length;
while(a = this.adapters[--i]) {
if(a.provide) return a;
}
}
},
_findNodeFor: {
value: function (anything) {
var node, i, adapters, adapter;
adapters = this.adapters;
// loop through adapters that have the findNode() method
// to try to find out which adapter and which node
i = 0;
while (!node && (adapter = adapters[i++])) {
if (adapter.findNode) {
node = adapter.findNode(anything);
}
}
return node && { node: node };
}
}
});
return Collection;
function extend(base, mixin) {
var extended = Object.create(base);
for(var p in mixin) {
extended[p] = mixin[p];
}
return extended;
}
});
}(typeof define === 'function' ? define : function(factory) { module.exports = factory(require); }));