-
Notifications
You must be signed in to change notification settings - Fork 1
/
multifilter.js
58 lines (47 loc) · 1.02 KB
/
multifilter.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
//v1.0.0
// ==========================================
// Copyright 2013 Dataminr
// Licensed under The MIT License
// http://opensource.org/licenses/MIT
// ==========================================
define([
'Backbone.CollectionFilter',
'underscore'
], function(CF) {
return function(collection, filters) {
var on = [];
if (filters.on) {
on = _.values(_.pick(filters, filter.on));
}
var filter = function(model) {
var length = on.length;
for (var i = 0; i < length; i++) {
if (!on[i](model))
return false;
}
return true;
};
var cf = new CF(collection, filter);
cf.addFilter = function(name) {
var fn = filters[name];
if (!fn)
return false;
if (on.indexOf(fn) > -1)
return true;
on.push(fn);
this.redoFilter();
this.trigger('reset');
return true;
};
cf.removeFilter = function(name) {
var fn = filters[name];
if (!fn)
return false;
on.splice(on.indexOf(fn), 1);
this.redoFilter();
this.trigger('reset');
return true;
};
return cf;
};
});