Mehdi
Mehdi

Reputation: 813

Showing a view by default in all routes in Backbone

I want a backbone view, which is my navigation to appear in all the routes. What would be the best way of doing this?

Ideally I would want a route where matches all given routes and fires up my "home" function which creates the navigation, something like this:

routes: {
        "*" : "home",
        "addnames" : "addNames"

    },

Upvotes: 2

Views: 113

Answers (1)

machineghost
machineghost

Reputation: 35735

While Deeptechtons is 100% correct in his comment, there are plenty of legitimate times when someone might want their routes to trigger some common logic or have access to some common object (like a toolbar).

Routes trigger "route" events when they're hit, so if all you need to do is trigger logic one simple way is to just:

router.on('route', functionThatDoesWhateverYouWantToDoOnEveryRoute);

If you need the view itself to have access to a common object, your best bet is probably to make all your route views extend from a base RouteView (and put the object in there). If you want to get hardcore (but most likely this is the wrong approach) you could even override one of the core Backbone router methods, and invoke the original version from inside of it using:

Backbone.Router.prototype.methodName.apply(this, arguments);

Hope that helps.

Upvotes: 1

Related Questions