singuerinc
singuerinc

Reputation: 487

Next callback in Backbone Router

Is it possible to navigate to the next route handler if the first handler doesn't satisfy/catch a specific situation?

Is something that you can achieve in Express (nodejs) with the next()method.

Imagine that you have this router configuration:

routes: {
    '*path': 'onPath',
    '*notFound': 'onNotFound'
},

onPath: function(path, next){
    if(path == 'something'){
        console.log('ok');
    } else {
        next();
    }
},

onNotFound: function(){
    console.log('KO');
}

I know that I can mix the onPath and onNotFound methods, but I just want do know if is possible. Thanks!

Upvotes: 0

Views: 164

Answers (1)

Erik Ahlswede
Erik Ahlswede

Reputation: 2155

First of all, I'm not sure you can have 2 path matchers in a router. How would the Router know which to use? Here is an option. Remove the notFound route and call the method directly :

routes: {
    '*path': 'onPath'
},

onPath: function(path){
    if(path == 'something'){
        console.log('ok');
    } else {
        this.onNotFound(path);
    }
},

onNotFound: function(path){
    console.log('KO');
}

Or an even cleaner approach: you can throw an event (avoid too many App level events if you can. This is just an example)

App.trigger("pathNotFound", path);

Somewhere else in your code (again, perhaps not at the App level), you will need to listen for this event:

App.listenTo("pathNotFound", function(){
               console.log('KO');
});

This is all roughly written. You will need to adapt it to your application of course.

Upvotes: 0

Related Questions