orange
orange

Reputation: 8090

Multiple route handlers in backbonejs

In my Backbone.js app, I have a route structure that has some common part which I'd like to handle in one handler only. For instance, these URLs

/#scenario/1/show-report
/#scenario/2/foo
/#scenario/3/bar

would all need to set the scenario to its particular identifier. At current, I do this in each route handler (in the handler for show-report, foo and bar). Is there any way of catching the URL up to scenario/:id/, calling the appropriate function and processing the rest by the specific handlers?

Upvotes: 0

Views: 145

Answers (1)

Vitalii Petrychuk
Vitalii Petrychuk

Reputation: 14225

var Router = Backbone.Router.extend({
    routes : {
        'scenario/:id/:type' : 'scenario'
    },
    scenario : function (id, type) {
        switch (type) {
            case 'show-report': console.log('show-report'); break;
            case 'foo':         console.log('foo'); break;
            case 'bar':         console.log('bar'); break;
        }
    }
});

new Router();
Backbone.history.start();

Upvotes: 2

Related Questions