简体   繁体   English

骨干路由器.navigate不重定向

[英]Backbone router.navigate doesn't redirect

Honestly I feel a bit stupid asking this question because should be a very simple thing... In my app init, I have: 老实说,我问这个问题有点愚蠢,因为应该是一件很简单的事情……在我的应用程序初始化中,我有:

window.myApp =
  Models: {}
  Collections: {}
  Views: {}
  Routers: {}
  initialize: ->
    window.router = new Backbone.Router

    Backbone.history.start(pushState: true)

And then in my View, when I click a button, I do: 然后在“视图”中,当我单击一个按钮时,我将执行以下操作:

router.navigate('/profile')

I have tried also with 我也尝试过

router.navigate('/profile', true)

Now...I know that this isn't the best way of use backbone routes, but I need that my rails app manage the routes and I use backbone routes only for have routes history... If I do window.href = '/profile' it redirects correctly. 现在...我知道这不是使用骨干路由的最佳方法,但是我需要我的Rails应用程序管理这些路由,并且我仅将骨干路由用于具有路由历史记录...如果我执行window.href =' / profile”,即可正确重定向。

What I'm doing wrong with backbone routes ? 我对骨干网路由做错了什么? I've used this way time ago, but I don't find out why now it doesn't work anymore. 我以前用过这种方式,但是我不知道为什么现在不再使用了。

EDIT: The new url is always correctly show in the navigation bar, but it doesn't redirect to the new page...only show the new url in the navigation bar. 编辑:新的URL总是正确显示在导航栏中,但它不会重定向到新页面...仅在导航栏中显示新的URL。

Backbone router.navigate doesn't redirect 骨干路由器.navigate不重定向

No, it doesn't. 不,不是。 That's not what router.navigate even does . 那不是router.navigate 所做的 It's has nothing to do with redirecting, it's for updating the address bar. 它与重定向无关,它用于更新地址栏。

The new url is always correctly show in the navigation bar, but it doesn't redirect to the new page...only show the new url in the navigation bar. 新网址始终正确显示在导航栏中,但不会重定向到新页面...仅在导航栏中显示新网址。

Yes, this is exactly what router.navigate is documented to do. 是的,这正是记录router.navigate的内容。 That is its express purpose. 这就是它的明确目的。 You use it to update the URL to reflect the current state of the page, not to redirect the page to something else. 您可以使用它来更新URL以反映页面的当前状态, 而不是将页面重定向到其他内容。

If you want to redirect to a new page, you're supposed to use window.location.href = '/profile' . 如果要重定向到新页面, window.location.href = '/profile'使用window.location.href = '/profile'

If you want to update the address bar and then trigger a fresh round of Backbone routing, use router.navigate('/profile', { trigger: true }); 如果要更新地址栏,然后触发新一轮的主干路由,请使用router.navigate('/profile', { trigger: true }); , but again, that's not how Backbone is meant to work. ,不过,这不是Backbone的工作方式。

We had an issue with this, and found out that sometimes even window.location.href does not work. 我们对此有一个问题,发现有时window.location.href有时不起作用。

We were lucky to find this post Backbone: Refresh the same route path for twice and we were actually only able to make it work using a combination of the presented solution : 我们很幸运找到这篇文章Backbone:将同一路径刷新两次 ,实际上我们只能使用所提出的解决方案使它起作用:

  1. Navigate to an inexistent route : Backbone.history.navigate('xpto'); 导航到一条不存在的路由: Backbone.history.navigate('xpto');

  2. Then use: window.location.href = location; 然后使用: window.location.href = location;

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

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