我注意到我正在构建的Backbone应用程序中有一点点怪癖,并且想知道这种行为是否是预期的,或者我是否做错了什么......
我像这样启动Backbone.history:
Backbone.history.start({
root: '/au/store/',
pushState: true,
silent: true
});
Run Code Online (Sandbox Code Playgroud)
要制作后退/前进按钮导航触发路由,我需要它们设置如下:
router = Backbone.Router.extend({
routes: {
'au/store/:slug' : 'slug',
'au/store/?*params' : 'params'
}
});
Run Code Online (Sandbox Code Playgroud)
这很好用.浏览浏览器历史记录以/au/store/?foo=bar
按预期触发"参数"路径.
我遇到的问题是router.navigate()
不会触发路线:
router.navigate('?foo=bar', {trigger:true}); // route doesn't trigger
Run Code Online (Sandbox Code Playgroud)
将根添加到URL也不起作用:
router.navigate('au/store/?foo=bar', {trigger:true}); // navigates to /au/store/au/store/?foo=bar
Run Code Online (Sandbox Code Playgroud)
所以我现在使用的解决方法是两次运行所有路由,一次以root为前缀,一次不用:
routes: {
'au/store/:slug' : 'slug',
'au/store/?*params' : 'params',
':slug' : 'slug',
'?*params' : 'params'
}
Run Code Online (Sandbox Code Playgroud)
现在它会在后退/前进时触发路由,也可以通过router.navigate()触发.
但这看起来像是一个黑客攻击,肯定会在更复杂的路线上引发问题......
任何人都可以向我解释我做错了什么,或者为什么它没有表现出我对它的期望?