forked from angelo0000/backbone_filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone_filters.js
37 lines (36 loc) · 1.2 KB
/
backbone_filters.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
(function() {
_.extend(Backbone.Router.prototype, Backbone.Events, {
before: {},
after: {},
_runFilters: function(filters, fragment, args) {
if (_(filters).isEmpty()) {
return true;
}
var failingFilter = _(filters).detect(function(func, filterRoute) {
if (!_.isRegExp(filterRoute)) {
// filterRoute = this._routeToRegExp(filterRoute);
filterRoute = new RegExp(filterRoute);
}
if (filterRoute.test(fragment)) {
var result = (_.isFunction(func) ? func.apply(this, args) : this[func].apply(this, args));
return _.isBoolean(result) && result === false;
}
return false;
},
this);
return failingFilter ? false : true;
},
route: function(route, name, callback) {
Backbone.history || (Backbone.history = new Backbone.History);
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
Backbone.history.route(route, _.bind(function(fragment) {
var args = this._extractParameters(route, fragment);
if (this._runFilters(this.before, fragment, args)) {
callback.apply(this, args);
this._runFilters(this.after, fragment, args);
this.trigger.apply(this, ['route:' + name].concat(args));
}
}, this));
}
});
}).call(this);