forked from dlikhten/filtered-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone-filtered-collection.js
138 lines (118 loc) · 4.92 KB
/
backbone-filtered-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
/*
Copyright (C) 2012 Dmitriy Likhten
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
(function(_, Backbone) {
var defaultFilter = function() {return true;};
/**
* This represents a filtered collection. You can either pass a filter or
* invoke setFilter(filter) to give a filter. The filter is identical to
* that which is used for _.select(array, filter)
*
* false filter indicates no filtering.
*
* do not modify this collection directly via #add/#remove, modify the
* underlying origModel.
*/
Backbone.FilteredCollection = Backbone.Collection.extend({
collectionFilter: null
,defaultFilter: defaultFilter
,initialize: function(models, data) {
if (models) throw "models cannot be set directly, unfortunately first argument is the models.";
this.collection = data.collection;
this.setFilter(data.collectionFilter);
this.collection.on("add", this.addModel, this);
this.collection.on("remove", this.removeModel, this);
this.collection.on("reset", this.resetCollection, this);
}
,_reset: function(options) {
Backbone.Collection.prototype._reset.call(this, options);
this._mapping = [];
}
,add: function() {
throw "Do not invoke directly";
}
,remove: function() {
throw "Do not invoke directly";
}
,reset: function() {
throw "Do not invoke directly";
}
,resetCollection: function() {
this._mapping = [];
this._reset();
this.setFilter(undefined, {silent: true});
this.trigger("reset", this);
}
,removeModel: function(model, colleciton, options) {
var at = this._mapping.indexOf(options.index);
if (at > -1) {
this._forceRemoveModel(model, _.extend({index: at}, options));
}
}
,_forceRemoveModel: function(model, options) {
this._mapping.splice(options.index, 1);
Backbone.Collection.prototype.remove.call(this, model, {silent: options.silent});
}
,addModel: function(model, collection, options) {
if (this.collectionFilter(model)) {
this._forcedAddModel(model, options);
}
}
,_forcedAddModel: function(model, options) {
var desiredIndex = options.index;
// determine where to add, look at mapping and find first object with the index
// great than the one that we are given
var addToIndex = _.sortedIndex(this._mapping, desiredIndex, function(origIndex) {
return origIndex;
});
// add it there
Backbone.Collection.prototype.add.call(this, model, {at: addToIndex, silent: options.silent});
this._mapping.splice(addToIndex, 0, desiredIndex);
}
,setFilter: function(newFilter, options) {
if (newFilter === false) { newFilter = this.defaultFilter } // false = clear out filter
this.collectionFilter = newFilter || this.collectionFilter || this.defaultFilter;
options || (options = {});
// this assumes that the original collection was unmodified
// without the use of add/remove/reset events. If it was, a
// reset event must be thrown, or this object's .resetCollection
// method must be invoked, or this will most likely fall out-of-sync
// why HashMap lookup when you can get it off the stack
var filter = this.collectionFilter;
var mapping = this._mapping;
// this is the option object to pass, it will be mutated on each
// iteration
var passthroughOption = _.extend({}, options);
this.collection.each(function(model, index) {
var foundIndex = mapping.indexOf(index);
passthroughOption.index = foundIndex == -1 ? this.length : foundIndex;
if (filter(model, index)) {
// if already added, no touchy
if (foundIndex == -1) {
this._forcedAddModel(model, passthroughOption);
}
}
else {
if (foundIndex > -1) {
this._forceRemoveModel(model, passthroughOption);
}
}
}, this);
}
});
})(_, Backbone);