-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-subset.js
224 lines (180 loc) · 6.67 KB
/
backbone-subset.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
(function() {
var __slice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this.Subset = (function() {
function Subset(options) {
if (options == null) options = {};
this.intermediateCollections = {};
this.source = options.source || new Backbone.Collection;
this.collection = options.collection || new options.source.constructor;
this.filters = new Subset.Filters(options.filters);
this.source.on('reset', this.filterAll, this);
this.source.on('add', this.modelAdded, this);
this.source.on('remove', this.modelRemoved, this);
this.source.on('change', this.modelChanged, this);
this.filters.on('all', this.filterAll, this);
this.filterAll();
}
Subset.prototype.filterAll = function(eventName) {
var oldCache, previous;
var _this = this;
if (eventName === 'change') return this;
oldCache = this.intermediateCollections;
this.intermediateCollections = {};
previous = this.source;
this.filters.each(function(filter, index) {
var intermediate;
intermediate = _this.intermediateCollections[filter.cid] = oldCache[filter.cid] || new _this.collection.constructor;
intermediate.reset(filter.select(previous));
return previous = intermediate;
});
this.collection.reset(previous.models);
return this;
};
Subset.prototype.after = function(filter) {
return this.intermediateCollections[filter.cid] || this.collection;
};
Subset.prototype.before = function(filter) {
var index, _ref;
index = this.filters.indexOf(filter);
if (index === 0) return this.source;
return this.intermediateCollections[(_ref = this.filters.at(index - 1)) != null ? _ref.cid : void 0] || this.collection;
};
Subset.prototype.modelAdded = function(model) {
if (this.filters.match(model)) return this.collection.add(model);
};
Subset.prototype.modelRemoved = function(model) {
return this.collection.remove(model);
};
Subset.prototype.modelChanged = function(model) {
if (this.source.get(model.id) && this.filters.match(model)) {
if (!this.collection.get(model.id)) return this.collection.add(model);
} else {
return this.collection.remove(model);
}
};
Subset.prototype.query = function() {
return _.filter(this.source.models, this.filters.match);
};
Subset.prototype.match = function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return (_ref = this.filters).match.apply(_ref, args);
};
return Subset;
})();
this.Subset.Filter = (function() {
__extends(Filter, Backbone.Model);
function Filter() {
Filter.__super__.constructor.apply(this, arguments);
}
Filter.prototype.defaults = {
'operator': '=='
};
Filter.prototype.match = function(model) {
return this.buildMatcher()(model);
};
Filter.prototype.select = function(collection) {
return _.filter(collection.models, this.buildMatcher());
};
Filter.prototype.buildMatcher = function() {
return this["match_" + (this.get('operator'))](this.get('attribute'), this.get('value'));
};
Filter.prototype['match_=='] = function(attribute, value) {
return function(model) {
return model.get(attribute) == value;
};
};
Filter.prototype['match_==='] = function(attribute, value) {
return function(model) {
return model.get(attribute) === value;
};
};
Filter.prototype.match_in = function(attribute, value) {
return function(model) {
return _(value).include(model.get(attribute));
};
};
Filter.prototype.match_any = function(attribute, value) {
return function(model) {
return _.intersection(model.get(attribute) || [], value).length > 0;
};
};
Filter.prototype.match_all = function(attribute, value) {
return function(model) {
return _.intersection(model.get(attribute) || [], value).length === value.length;
};
};
return Filter;
})();
this.Subset.Filters = (function() {
__extends(Filters, Backbone.Collection);
function Filters() {
this.match = __bind(this.match, this);
Filters.__super__.constructor.apply(this, arguments);
}
Filters.prototype.model = Subset.Filter;
Filters.prototype.initialize = function() {
return this.on('all', this.clearCache, this);
};
Filters.prototype.matchers = function() {
return this.map(function(filter) {
return filter.buildMatcher();
});
};
Filters.prototype.buildMatcher = function() {
var matchers;
matchers = this.matchers();
return function(model) {
var matcher, _i, _len;
for (_i = 0, _len = matchers.length; _i < _len; _i++) {
matcher = matchers[_i];
if (!matcher(model)) return false;
}
return true;
};
};
Filters.prototype.match = function(model) {
return this.buildMatcher()(model);
};
return Filters;
})();
this.Union = (function() {
function Union(options) {
var source, _i, _len, _ref;
this.collection = options.collection || new Backbone.Collection();
this.sources = options.sources;
_ref = this.sources;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
source = _ref[_i];
source.on('add', this.addItem, this);
source.on('remove', this.removeItem, this);
source.on('reset', this.reset, this);
}
this.reset();
}
Union.prototype.addItem = function(model) {
if (!this.collection.get(model.id)) return this.collection.add(model);
};
Union.prototype.removeItem = function(model) {
if (!_(this.sources).any(function(source) {
return source.get(model.id);
})) {
return this.collection.remove(model);
}
};
Union.prototype.reset = function() {
var s;
return this.collection.reset(_.flatten((function() {
var _i, _len, _ref, _results;
_ref = this.sources;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
s = _ref[_i];
_results.push(s.toArray());
}
return _results;
}).call(this)));
};
return Union;
})();
}).call(this);