How do I disable backbone history but still allow hash-based routing?

瘦欲@ 提交于 2019-12-04 11:20:30

问题


Say I do the following:

  • Click a link on the homepage (/) and go to /posts/1
  • Trigger an event and go to the backbone route /posts/1/#/1/edit
  • I click back

I need to make it so that the user ends up back on the homepage (/) not back at /posts/1

So I need to allow for backbone hash routes to work but not modify the history. I'd personally prefer to keep the history, but it's a requirement of a project.


回答1:


The latest version of Backbone (0.9.x) has the ability to trigger a route, but not add it to the history.

See Backbone.Router#navigate for the replace:true option.

Basically, just call .navigate on your router with trigger:true (to fire the route) and replace:true (to prevent it going to history)

app.navigate('posts/1/edit',{trigger:true, replace: true});

Here's a jsfiddle showing it in action: http://jsfiddle.net/7Z6ju/1/

  • Click "Post 1" to go to the Post 1 page.
  • Then, click "Edit" to go to the edit page.
  • Then, hit the back button - you should end up back on home.


来源:https://stackoverflow.com/questions/9338207/how-do-i-disable-backbone-history-but-still-allow-hash-based-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!