简体   繁体   English

骨干路由器不触发路由

[英]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 当我在http:// [...] / #inbox上,我打电话

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? 现在我已经看过Backbone的来源(https://github.com/documentcloud/backbone/blob/master/backbone.js#L1027),我发现它不支持我想要做的但是有一些方法可以实现这个目标吗?

I would create an event manager in your engage.app object, like this: 我会在你的engage.app对象中创建一个事件管理器,如下所示:

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. 并在engage.app对象中处理该事件,执行以前在路由处理程序中的代码。

Now, instead of calling appRouter.navigate you can trigger that same event. 现在,您可以触发相同的事件,而不是调用appRouter.navigate

Also, from that handler, you can call appRouter.navigate('inbox'); 此外,从该处理程序,您可以调用appRouter.navigate('inbox'); without passing true. 没有传递真实。 Now you can get your app to the state you want without trying to force the route. 现在,您可以将应用程序设置为所需的状态,而不必尝试强制路由。

As far as I can tell... As of Backbone 0.9.10 using appRouter.navigate('inbox', {trigger: true}); 据我所知...截至Backbone 0.9.10使用appRouter.navigate('inbox', {trigger: true}); works as expected. 按预期工作。

另一种选择就是调用该方法

appRouter.inbox();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM