Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Router not triggering route

I have the following router:

appRouter = Backbone.Router.extend({
  routes: {
    '': 'inbox',
    'inbox': 'inbox',
    'discussions_engagement': 'contacts',
  },

  inbox: function(page) {
    console.log('inbox');
    var page = page || 1;
    engage.app.hydrateInbox(page, engage.app.showInbox);
  },
  ....
};

When I am on http://[...]/#inbox and I call

appRouter.navigate('inbox', {trigger: true});

the inbox action doesn't fire which is what I want to achieve. Now I have looked at the source of Backbone (https://github.com/documentcloud/backbone/blob/master/backbone.js#L1027) and I see that it doesn't support what I'm trying to do but is there some way of accomplishing this?

like image 961
Gavin Schulz Avatar asked Feb 25 '12 21:02

Gavin Schulz


1 Answers

I would create an event manager in your engage.app object, like this:

var vent = _.extend({}, Backbone.Events);

Then in your router do this for the inbox route:

vent.trigger('inbox:show', page);

And handle that event in the engage.app object, doing the code there that used to be in the route handler.

Now, instead of calling appRouter.navigate you can trigger that same event.

Also, from that handler, you can call appRouter.navigate('inbox'); without passing true. Now you can get your app to the state you want without trying to force the route.

like image 63
Paul Hoenecke Avatar answered Sep 28 '22 06:09

Paul Hoenecke