backbone-async-route-filters

async before and after filters for backbone routes

npm install backbone-async-route-filters
3 downloads in the last week
6 downloads in the last month

Backbone Async Route filters

Backbone Async Route filters allows you to have a pre-condition for the router using before filters and some "after" routing calls using after filters.

Backbone async filters is port of fantactuka/backbone-route-filter with expressjs style async next callbacks.

Installation

Using Bower bower install backbone-async-route-filter or just copy backbone-async-route-filter.js

Usage

var Router = Backbone.Router.extend({
  routes: {
    'users': 'usersList',
    'users/:id': 'userShow',
    'account/sign-in': 'signIn'
  },

  before: {
    // Using instance methods
    'users(:/id)': 'checkAuthorization',

    // Using inline filter definition
    '*any': function(fragment, args, next) {
      console.log('Atempting to load ' + fragment + ' with arguments: ', args);
      next();
    }
  },

  after: {
    // Google analytics tracking
    // After filter will be triggered only if all before filters passed and action was triggered,
    // so you'll only track pages that was displayed to user
    '*any': function(fragment, args, next) {
      goog._trackPageview(fragment);
      next();
    }
  },

  checkAuthorization: function(fragment, args, next) {

    //make ajax to check authorization here.
    $.ajax({
      data: somedata,
      success: function() {
        // if logged in execute next() to move ahead.
        next();
      },
      error: function() {
        //redirect to signin page.
      }
    });

  }
});

LICENSE

MIT

npm loves you