-
Notifications
You must be signed in to change notification settings - Fork 1
/
cf.js
171 lines (153 loc) · 4.86 KB
/
cf.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
//v1.1.0
// ==========================================
// Copyright 2013 Dataminr
// Licensed under The MIT License
// http://opensource.org/licenses/MIT
// ==========================================
define([
'underscore',
'backbone'
], function(_, Backbone) {
var noBind = ['on', 'trigger', 'off', 'at', 'pluck', 'forEach', 'each',
'map', 'reduce', 'reduceRight', 'find', 'detect', 'filter', 'select',
'reject', 'every', 'all', 'some', 'any', 'include', 'contains',
'invoke', 'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size',
'first', 'initial', 'rest', 'last', 'without', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty', 'groupBy', 'toJSON'];
var proto = {
_filter: null,
comparator: null,
/**
* runs whenever there is an event on the parent collection
*/
onFilter: function(type, data) {
type = type || '';
// don't pass along remove if the model isn't in the filter
if (type === 'remove' && ! _.contains(this.models, data)) {
return;
} else if (type == 'remove' && this.simple) {
this.models.splice(this.indexOf(data), 1);
this.models.sort(this.comparator);
}
var filt;
if (type === 'add' || type.indexOf('change') === 0)
filt = this._filter(data);
// don't pass along add if the model doesn't pass the filter
if (type == 'add') {
if (!filt) {
return;
} else if (this.simple) {
this.models.push(data);
this.models.sort(this.comparator);
}
}
// if a change is made to a model that changes whether it is in
// the collection then fire either add or remove
if (type.indexOf('change') === 0 &&
_.contains(this.models, data) != filt) {
if (!this.simple)
this.redoFilter();
if (filt) {
this.trigger('add', data);
if (this.simple) {
this.models.push(data);
this.models.sort(this.comparator);
}
} else {
this.trigger('remove', data);
if (this.simple) {
this.models.splice(this.indexOf(data), 1);
this.models.sort(this.comparator);
}
return;
}
}
// reset the models on any other event (just in case)
if ((!this.simple && _.contains(this._filterChangeEvents, type)) ||
(this.simple && type == 'reset')) {
this.redoFilter();
}
// pass along the event
this.trigger(type, data);
},
/**
* reset the models based on the filter and sort
*/
redoFilter: function() {
this.models = this.collection.filter(this._filter);
this.models.sort(this.comparator);
this.length = this.models.length;
}
};
/**
* will give back a function that will bind only called in a specific context
* @param {Function} fn original function
* @param {Object} bindee context to bind to
* @param {Object} binder if called with this context then will bind
* @return {Function}
*/
var bindIf = function(that, fn, bindee) {
var getFn = function() {
var proto = Object.getPrototypeOf(that);
while (proto && !proto[fn]) {
proto = Object.getPrototypeOf(proto);
}
return proto && proto[fn];
};
return function() {
return getFn().apply((
this == that ?
bindee :
this), [].slice.call(arguments));
};
};
/**
* give a collection and filter function (and options) return the filtered collection
*/
Backbone.CF = function(collection, filter, options) {
options = _.extend({}, options);
var leaveBind = _.union(noBind, options.noBind);
// setuo new collection
var Filtered = function() {};
Filtered.prototype = collection;
var filtered = new Filtered();
filtered._filterChangeEvents = options.overrideDefaultEvents ? [] : [
'change',
'reset',
'add',
'remove',
'sort'
];
filtered.simple = options.simple;
if (_.isArray(options.filterChangeEvents))
filtered._filterChangeEvents =
filtered._filterChangeEvents.concat(options.filterChangeEvents);
// add in instance variables
_.extend(filtered, proto);
filtered.collection = collection;
filtered._filter = filter;
var comparator = options.comparator ||
collection.comparator ||
function (a, b) {
return collection.indexOf(a) - collection.indexOf(b);
};
filtered._callbacks = {};
filtered._boundFns = [];
// bind functions from parent
for (var name in collection) {
if (proto[name] === undefined &&
!_.contains(leaveBind, name) &&
_.isFunction(collection[name])) {
filtered._boundFns.push(name);
filtered[name] = bindIf(filtered, name, collection);
}
}
// bind comparator
filtered.comparator = _.bind(comparator, filtered);
filtered._events = {};
// setup and run the filter
filtered.redoFilter();
collection.on('all', filtered.onFilter, filtered);
return filtered;
};
});