LordZardeck
LordZardeck

Reputation: 8283

backbone.js router with multiple actions

Is it possible to have backbone.js's routing feature determine multiple actions? for example, my single page app can have multiple tabs open with each tab doing different actions. Can this be done, and if so, how?

Upvotes: 1

Views: 1216

Answers (1)

SunnyRed
SunnyRed

Reputation: 3545

If I understand you correctly this can not be achieved out of the box as Backbones routing mechanism maps each route to a single callback. From the 0.9.1 docs

//Implementation
route: function(route, name, callback) {}

//Usage: "Manually bind a single named route to a callback. For example:"
this.route('search/:query/p:num', 'search', function(query, num) {
     ...
});

So if you need to pass multiple callbacks you need to override the route function to handle an array of callbacks - or you can post an issue at github to motivate its implementation.

A more pragmatic approach to your questions is of course to handle the mapping outside of the route definition, like

  routes: {
    "routeA": "actionA",
    "routeB": "actionB",
    "routeC": "actionCompound"
  },
  actionA: function() {
    //...
  },
  actionB: function() {
    //...
  },
  actionCompound: function() {
    this.actionA();
    this.actionB();
  }

Upvotes: 3

Related Questions