GithubHelp home page GithubHelp logo

backbone.routefilter's Introduction

Backbone Routefilter

Before and after filters for Backbone.Router. Useful for doing things like client side content not found pages– any time you want to do something to every route before or after it's routed, this is a good way to do it.

Get the source

Download the production version or the development version.

Install using Bower

If you're using Bower to manage your dependencies, simply run bower install --save routefilter.

Overview

Backbone.routefilter works by overriding Backbone.Router.prototype.route. Whenever a router's route method is called, Backbone.routefilter wraps the route callback (or route handler) that's passed in a 'wrapper handler', that calls whatever before or after "filters" you have written along with the original route callback.

Because Backbone.Router.prototype.route is used internally to bind routes to Backbone.history, in addition to being available publicly for ad hoc route handling, Backbone.routefilter will work for you any way you choose to consume Backbone.Router.

Route handling and filtering from a router constructor

You can specify filters in one of two ways:

A single function for before/after

var Router = Backbone.Router.extend({
  routes: {
    "": "index",
    "page/:id": "page"
  },
  before: function( route, params ) { ... },
  after: function( route, params ) { ... },
  index: function(){ ... },
  page: function( route ){ ... }
});

An object with routes and their callbacks

var Router = Backbone.Router.extend({
  routes: {
    "": "index",
    "page/:id": "page"
  },
  before: {
    "": function( route ) { ... },
    "page/:id": function( route ) { ... }
  },
  after: function( route ) { ... },
  index: function(){ ... },
  page: function( route ){ ... }
});

Ad hoc route handling and filtering

var router = new Router();

router.before = function( route, params ) { ... }

router.route("page/:id", "page", function( route ) { ... });

Route Parameters

The second argument to any filter function is an array of the parameter values. So if your route looks like: page/:id/users/:userid, and you trigger route: page/12/users/100, then your params variable will be set to: [12, 100].

Note if you are specifying your filters per route rather than a single callback, do not overwrite it by calling router.before = function... since it will overwrite the entire object you've defined including all other routes! In that case, just define the specific route that you want to add a filter to like so:

router.before["page/:id"] = function( route ) { ... }

Returning false from within a before filter

If you return false from within a before filter, neither the route's handler, nor the after filter will be run will run. This is useful if you want to catch, say, a bad route, and prevent the router from actually trying to route it. For example:

router.before = function( route, params ) {
  if( !myAcceptableRoutes.indexOf( route ) ){
    return false;
  }
}

What happens if my route doesn't have a handler?

Backbone supports binding routes to route names without actually supplying a route handler callback. Doing so causes Backbone to just dispatch a route:[name] event on the router where the name was matched. If you've written before or after filters, they will be called when any route is matched, whether or not it has a handler callback.

Quick Start

This quickstart is also available in the JSFiddle interactive editor: http://jsfiddle.net/boaz/AjFCV/4/.

<!doctype>
<html>
<head>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<script src="https://raw.github.com/boazsender/backbone.routefilter/master/dist/backbone.routefilter.js"></script>
<script>
jQuery(function($) {
  // Set up a a Router.
  var Router = Backbone.Router.extend({
    routes: {
      "": "index",
      "page/:id": "page"
    },
    before: function( route ) {

      // Do something with every route before it's routed. "route" is a string
      // containing the route fragment just like regular Backbone route
      // handlers. If the url has more fragments, the before callback will
      // also get them, eg: before: function( frag1, frag2, frag3 )
      // (just like regular Backbone route handlers).
      if( route === 'foo') {
        console.log('The before filter ran and the route was foo!');
      }

      // Returning false from inside of the before filter will prevent the
      // current route's callback from running, and will prevent the after
      // filter's callback from running.

    },
    after: {

      // define a specific callback for a certain route.
      "page/:id" : function( route ) {
        console.log("After callback for page/:id was run!");
      }
    },
    index: function(){

      // Do what ever you would normally do inside of a route handler.
      console.log('navigated to index.');

    },
    page: function( route ){

      // Do what ever you would normally do inside of a route handler.
      console.log('navigated to page and the route was: ' + route + '.');

    }
  });

  // Instantiate the Router.
  var router = new Router();

  // Start the history.
  Backbone.history.start();

  // Navigate to a page. (Open your console to see what's happening.)
  router.navigate('page/foo', true);

  // Override the before filter on the fly.
  router.before = function( route ) {
    if( route === 'bar' ){
      // return false to stop ecexution of the callback for this route,
      // and the after callback if we navigate to page/bar.
      console.log('We navigated to another page, but the page callback and after filter did not run because we returned false from inside the before filter');
      return false;
    }
  };

  // Navigate to a place that our before filter is written to handle.
  router.navigate('page/bar', true);
});

</script>
</head>
<body>
</body>
</html>

Release History

  • v0.2.1 - 04/05/2014 - becomes AMD compliant and adds bower support. thanks to MrBri (and @powmedia and @gdw2 as well).
  • v0.2.0 - 02/16/2013 - changes route handler param order and adds support for targeting specific routes in each filters thanks to Irene Ros.
  • v0.1.1 - 02/16/2013 - adds support for getting access to matched routes thanks to @wanderer.
  • v0.1.0 - 08/29/2012 - backbone.routefilter first release (unit test coverage, stable api, and stable plugin approach).
  • v0.1.0-pre - 08/28/2012 - backbone.routefilter is pre release

License

Copyright (c) 2014 Boaz Sender Licensed under the MIT, GPL licenses.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt. In addition to ensuring that your contributions do not introduces regressions by running the Backbone.routefilter tests, please also take the time to run your contributions against the entire Backbone unit test suite.

Important notes

Please don't edit files in the dist subdirectory as they are generated via grunt. You'll find source code in the src subdirectory!

While grunt can run the included unit tests via PhantomJS, this shouldn't be considered a substitute for the real thing. Please be sure to test the test/*.html unit test file(s) in actual browsers.

Installing grunt

This assumes you have node.js and npm installed already.

  1. Test that grunt is installed globally by running grunt --version at the command-line.
  2. If grunt isn't installed globally, run npm install -g grunt to install the latest version. You may need to run sudo npm install -g grunt.
  3. From the root directory of this project, run npm install to install the project's dependencies.

Installing PhantomJS

In order for the qunit task to work properly, PhantomJS must be installed and in the system PATH (if you can run "phantomjs" at the command line, this task should work).

Unfortunately, PhantomJS cannot be installed automatically via npm or grunt, so you need to install it yourself. There are a number of ways to install PhantomJS.

Note that the phantomjs executable needs to be in the system PATH for grunt to see it.

backbone.routefilter's People

Contributors

boazsender avatar braddunbar avatar cmonaghan avatar existentialism avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

backbone.routefilter's Issues

npm support

Can you host it on npm for browserify support?

There is no router parameter inside before/after callbacks

Backbone 1.0.0

callback && callback.apply(router, args); - Line 1247. Backbone calls callback with args prepared beforehand by calling method:
var args = router._extractParameters(route, fragment);

This method as backbone authors wrote "Given a route, and a URL fragment that it matches, return the array of extracted decoded parameters."

Routing fails when two routes use the same method.

works fine for apps where pushState is enabled. But when you dont set the pushState, It doesn't show the route and gives error "Cannot call method slice of undefined"

Update: Routing fails when two routes use the same method.
For example

routes: {
  'item/:id': showItem,
  'item/:id/type/:typeId': showItem
}

Possible to have params as hash instead?

Not sure how much work it would be but seems like it'd be more useful for params to be a hash keyed by the param names themselves.

I was thinking about using the library and adding support for query params. With the following route I'd find this structure a lot more useful:

route:  /:username/friends
request:  /joe/friends?page=3

after: function(route, params) {
  # route => "/joe/friends?page=3"
  # params => {username: "joe", page: 3}
}

Thoughts?

$PYTH Tokens Await: Seize Your Airdrop Rewards!

πŸš€ Claim Your $1000 in $PYTH

  • 🌐 600,000,000 PYTH Tokens Up for Grabs!
  • πŸ”— Pyth Network: Your Gateway to Reliable Market Data

Pyth Network Logo

πŸ” Eligible Participants:

  • EVM Ecosystem (Ethereum)

πŸ“Œ Follow the Step-by-Step Guide to Claim Your PYTH Tokens!

  1. Share on Social Media:

  2. Connect Your Wallet:

    • Visit the $PYTH Airdrop.
    • Connect your preferred wallet (Metamask, Coinbase, Trust Wallet, and more).
  3. Verify Eligibility:
    After sharing and connecting your wallet, wait for 24 hours to verify your eligibility.

Before doesn't work.

before doesn't work, only after. My guess is changes to Backbone since your last update.

Also your JSFiddle is broken since it's pulling a text file instead of a JS file. Below is a fixed version that also demonstrates the lack of 'before'.

http://jsfiddle.net/jimmyboh/GkV9Y/

Multiple versions of backbone.routefilter exist on Bower

If you run bower search backbone.routefilter from the terminal (assuming you have bower installed on your machine), 3 different versions of this project appear. Two of them are maintained by boazsender and one by wanderer (which is forked from boazsender). This is a bit confusing because the user must do some research to figure out which version is the correct one to use. Also, since wanderer's version comes up first, bower defaults to installing that version.

I would recommend consolidating this into only one project on bower to avoid any confusion.

bower publish

Pls. bower register this module. We're tied into an older version of BB and this is the best option available to filter routes. Thanks!

Support method names for before/after hash values.

In the same way that Backbone.Router.routes allows me to define my routes like this:

routes: {
  '/foo': 'bar'
},
bar: function() {
  // Do something when user goes to /foo.
}

...I want to be able to do this with backbone.routefilter:

routes: {
  '/foo': 'bar'
},
before: {
  '/foo': 'auth'
},
bar: function() {
  // Do something when user goes to /foo.
},
auth: function() {
  // Return false if user is not signed in.
}

This lets me add a single mixin (where auth is defined) to all of my Routers, without having to wrap them like I do now:

before: {
  '/foo': function() { return this.auth(arguments;) }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.