【发布时间】:2012-02-21 04:23:54
【问题描述】:
我注意到我正在构建的 Backbone 应用程序中存在一些小问题,我想知道这种行为是否意料之中,或者我做错了什么...
我像这样启动 Backbone.history:
Backbone.history.start({
root: '/au/store/',
pushState: true,
silent: true
});
要制作后退/前进按钮导航触发路线,我需要像这样设置它们:
router = Backbone.Router.extend({
routes: {
'au/store/:slug' : 'slug',
'au/store/?*params' : 'params'
}
});
这很好用。浏览浏览器历史记录到 /au/store/?foo=bar 会按预期触发“参数”路由。
我遇到的问题是 router.navigate() 不会触发路由:
router.navigate('?foo=bar', {trigger:true}); // route doesn't trigger
将根添加到 url 也不起作用:
router.navigate('au/store/?foo=bar', {trigger:true}); // navigates to /au/store/au/store/?foo=bar
所以我目前使用的解决方法是运行所有路由两次,一次以根为前缀,一次不带:
routes: {
'au/store/:slug' : 'slug',
'au/store/?*params' : 'params',
':slug' : 'slug',
'?*params' : 'params'
}
现在它触发后退/前进路由以及通过 router.navigate()。
但这似乎有点像 hack,并且肯定会在更复杂的路线上导致问题......
谁能向我解释我做错了什么,或者为什么它的行为不像我期望的那样?
【问题讨论】:
-
所以这个问题在 Backbone 0.9.2 中得到了修复,现在它可以正常工作了(正如这个问题的两个答案中所描述的那样。)
标签: javascript ajax backbone.js router