Backbone.js 中调用的重复路由
我想要一种如下所示的 RESTful URL 结构:
- /accounts
- /accounts/account/123
我已经这样设置了我的路由:
MyRouter = Backbone.Router.extend({ routes : { '/accounts': 'accounts', '/accounts/account/:account': 'account', }, accounts: function() { console.log('accounts CALLED'); }, account: function() { console.log('account CALLED'); }, });
问题是当我转到 /accounts/account/ 时123 , accounts() 和 account() 都被调用(如URL 与两者都匹配路线)。我尝试了诸如 /accounts$ 之类的路由,但看起来路由哈希不支持它。
有办法做到这一点吗?手动 router.route(route, name,callback) 可以代替吗(尽管我真的不想这样做)。
I want to have a kind of RESTful URL structure like below:
- /accounts
- /accounts/account/123
I've set up my routes as such:
MyRouter = Backbone.Router.extend({ routes : { '/accounts': 'accounts', '/accounts/account/:account': 'account', }, accounts: function() { console.log('accounts CALLED'); }, account: function() { console.log('account CALLED'); }, });
The problem, is when I go to /accounts/account/123 , both accounts() and account() get called (as the URL matches both routes). I tried a route such as /accounts$, but it doesn't look like it's supported in the routes hash.
Is there a way to accomplish this? Would a manual router.route(route, name, callback) work instead (although I really prefer not to do that).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过另一个问题解决了这个问题。我没有意识到我必须严格使用router.navigate函数。
以编程方式使用路由器(而不是通过浏览器栏),那些重复的调用就会消失。我还看到使用 router.navigate 时仅调用一次预期函数。
我仍然需要找出如何捕捉回来&前进按钮来调用这些函数。感谢迄今为止的反馈。
I got this cleared up by another SO question. I didn't realise that I had to use the router.navigate function strictly.
Using the router programmatically (not via browser bar), those duplicate calls go away. I'm also seeing the expected functions called only once... when using router.navigate.
I still have to find out how to capture back & forward buttons to call those functions. Thanks for the feedback so far.
尝试使用 /accounts$ 代替。 $ 是字符串结尾的正则表达式。
Try /accounts$ instead. $ is the regex for the end of the string.