Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from danharper/master

Updated for Backbone 0.5.3
  • Loading branch information...
commit 67cecb3bbc03651f5aac5da6ff856b98f58a6811 2 parents 806276c + b4c698f
@angelo0000 authored
Showing with 62 additions and 54 deletions.
  1. +0 −20 README
  2. +28 −0 README.md
  3. +34 −34 backbone_filters.js
View
20 README
@@ -1,20 +0,0 @@
-Disclaimer:
-This plugin only works against pre 0.5 versions of Backbone.
-
-Usage:
-
-In your controller you can now add:
-
-before: {
- 'some_route_regex' : 'before_func',
- 'some_other_route_regex' : function(){},
-},
-
-after: {
- 'some_route_regex' : 'after_func',
- 'some_other_route_regex' : function(){},
-}
-
-Your filters will be called and if a filter returns false, the filter chain is halted.
-If a before filter chain is halted, the action in the controller will not be called. Your
-filters will receive the same arguments that get passed to the actions.
View
28 README.md
@@ -0,0 +1,28 @@
+_Disclaimer: This fork has been updated to work with Backbone 0.5.3_
+
+# Usage
+
+Include `backbone_filters.js` after Backbone.
+
+In your router you can now add:
+
+```javascript
+before: {
+ '^clerks' : function() {
+ /* do stuff to all routes starting with 'clerks' */
+ /* return false to halt execution */
+ },
+ 'another reg ex' : function() { }
+},
+
+after: {
+ '^clerks' : function() {
+ /* do stuff */
+ },
+ 'another reg ex' : function() { }
+}
+```
+
+Your filters will be called and if a filter returns false, the filter chain is halted.
+If a before filter chain is halted, the action in the Router will not be called. Your
+filters will receive the same arguments that get passed to the actions.
View
68 backbone_filters.js
@@ -1,37 +1,37 @@
(function() {
- _.extend(Backbone.Controller.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);
- }
- 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);
+ _.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));
- }
- });
+ 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);
Please sign in to comment.
Something went wrong with that request. Please try again.