The main router does not start the 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 in the folder http: // [...] / # and I call

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

the inbox action does not work out what I want to achieve. Now I looked at the source of Backbone (https://github.com/documentcloud/backbone/blob/master/backbone.js#L1027) and I see that it does not support what I'm trying to do, but is there any way to do this is?

+6
source share
3 answers

I would create an event dispatcher in your action.app object, for example:

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

Then on your router, do this for the incoming mail route:

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

And handle this event in the activate.app object, executing the code there that was used in the route handler.

Now, instead of calling appRouter.navigate you can fire the same event.

In addition, from this handler, you can call appRouter.navigate('inbox'); without passing true. Now you can get your application in the state you need without trying to force a route.

+4
source

As far as I can tell ... Compared to Backbone 0.9.10 using appRouter.navigate('inbox', {trigger: true}); works as expected.

+2
source

Another option is to just call the method

 appRouter.inbox(); 
-1
source

Source: https://habr.com/ru/post/909364/


All Articles