forked from p3drosola/Backbone.VirtualCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.virtual-collection.js
208 lines (177 loc) · 6.53 KB
/
backbone.virtual-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
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
// Available under the MIT License (MIT)
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['backbone', 'underscore'], function (Backbone, _) {
Backbone.VirtualCollection = factory(root, Backbone, _);
root.VirtualCollection = Backbone.VirtualCollection;
});
} else if (typeof exports !== 'undefined') {
var Backbone = require('backbone');
var _ = require('underscore');
module.exports = factory(root, Backbone, _);
} else {
root.Backbone.VirtualCollection = factory(root, root.Backbone, root._);
root.VirtualCollection = root.Backbone.VirtualCollection;
}
}(this, function (root, Backbone, _) {
var VirtualCollection = Backbone.Collection.extend({
constructor: function (collection, options) {
options = options || {};
this.collection = collection;
if (options.comparator !== undefined) this.comparator = options.comparator;
if (options.close_with) this.bindLifecycle(options.close_with, 'close'); // Marionette 1.*
if (options.destroy_with) this.bindLifecycle(options.destroy_with, 'destroy'); // Marionette 2.*
if (!this.model) this.model = collection.model;
this.accepts = VirtualCollection.buildFilter(options.filter);
this._rebuildIndex();
this.listenTo(this.collection, 'add', this._onAdd);
this.listenTo(this.collection, 'remove', this._onRemove);
this.listenTo(this.collection, 'change', this._onChange);
this.listenTo(this.collection, 'reset', this._onReset);
this.listenTo(this.collection, 'sort', this._onSort);
this.initialize.apply(this, arguments);
},
// Marionette 1.*
bindLifecycle: function (view, method_name) {
view.on(method_name, _.bind(this.stopListening, this));
},
updateFilter: function (filter) {
this.accepts = VirtualCollection.buildFilter(filter);
this._rebuildIndex();
this.trigger('filter', this, filter);
this.trigger('reset', this, filter);
return this;
},
_rebuildIndex: function () {
this._reset();
this.collection.each(function (model, i) {
if (this.accepts(model, i)) {
this.models.push(model);
this._byId[model.cid] = model;
if (model.id) this._byId[model.id] = model;
}
}, this);
this.length = this.models.length;
if (this.comparator) this.sort({silent: true});
},
orderViaParent: function (options) {
this.models = this.collection.filter(function (model) {
return (this._byId[model.cid] !== undefined);
}, this);
if (!options.silent) this.trigger('sort', this, options);
},
_onSort: function (collection, options) {
if (this.comparator !== undefined) return;
this.orderViaParent(options);
},
_onAdd: function (model, collection, options) {
if (this.accepts(model, options.index)) {
this._indexAdd(model);
this.trigger('add', model, this, options);
}
},
_onRemove: function (model, collection, options) {
if (!this.get(model)) return;
var i = this._indexRemove(model)
, options_clone = _.clone(options);
options_clone.index = i;
this.trigger('remove', model, this, options_clone);
},
_onChange: function (model, options) {
if (!model || !options) return; // ignore malformed arguments coming from custom events
var already_here = this.get(model);
if (this.accepts(model, options.index)) {
if (already_here) {
this.trigger('change', model, this, options);
} else {
this._indexAdd(model);
this.trigger('add', model, this, options);
}
} else {
if (already_here) {
var i = this._indexRemove(model)
, options_clone = _.clone(options);
options_clone.index = i;
this.trigger('remove', model, this, options_clone);
}
}
},
_onReset: function (collection, options) {
this._rebuildIndex();
this.trigger('reset', this, options);
},
sortedIndex: function (model, value, context) {
var iterator = _.isFunction(value) ? value : function(target) {
return target.get(value);
};
if (iterator.length == 1) {
return _.sortedIndex(this.models, model, iterator, context);
} else {
return sortedIndexTwo(this.models, model, iterator, context);
}
},
_indexAdd: function (model) {
if (this.get(model)) return;
var i;
// uses a binsearch to find the right index
if (this.comparator) {
i = this.sortedIndex(model, this.comparator, this);
} else if (this.comparator === undefined) {
i = this.sortedIndex(model, function (target) {
//TODO: indexOf traverses the array every time the iterator is called
return this.collection.indexOf(target);
}, this);
} else {
i = this.length;
}
this.models.splice(i, 0, model);
this._byId[model.cid] = model;
if (model.id) this._byId[model.id] = model;
this.length += 1;
},
_indexRemove: function (model) {
var i = this.indexOf(model);
if (i === -1) return i;
this.models.splice(i, 1);
delete this._byId[model.cid];
if (model.id) delete this._byId[model.id];
this.length -= 1;
return i;
}
}, { // static props
buildFilter: function (options) {
if (!options) {
return function () {
return true;
};
} else if (_.isFunction(options)) {
return options;
} else if (options.constructor === Object) {
return function (model) {
return !Boolean(_(Object.keys(options)).detect(function (key) {
return model.get(key) !== options[key];
}));
};
}
}
});
// methods that alter data should proxy to the parent collection
_.each(['add', 'remove', 'set', 'reset', 'push', 'pop', 'unshift', 'shift', 'slice', 'sync', 'fetch'], function (method_name) {
VirtualCollection.prototype[method_name] = function () {
return this.collection[method_name].apply(this.collection, _.toArray(arguments));
};
});
/**
Equivalent to _.sortedIndex, but for comparators with two arguments
**/
function sortedIndexTwo (array, obj, iterator, context) {
var low = 0, high = array.length;
while (low < high) {
var mid = (low + high) >>> 1;
iterator.call(context, obj, array[mid]) > 0 ? low = mid + 1 : high = mid;
}
return low;
}
_.extend(VirtualCollection.prototype, Backbone.Events);
return VirtualCollection;
}));