Backbone.js为其Backbone.router对象提供了一个导航(片段,[选项])方法,它执行以下操作(根据文档):
Whenever you reach a point in your application that you’d like to save as a URL, call
navigate
in order to update the URL. If you wish to also call the route function, set thetrigger
option totrue
. To update the URL without creating an entry in the browser’s history, set thereplace
option totrue
.
因此,只需调用appRouter.navigate(‘page’);可能会将URL更改为www.myapp.com/page而不会触发相应的路径.但是,我发现我的路由器重定向到URL并且还触发了页面路由,尽管默认情况下trigger = false.
因此,以下代码:
$(function(){
var AppRouter = Backbone.Router.extend({
routes: {
'': 'home',
'page': 'page',
},
home: function() {
window.app.navigate('page', {replace: true});
console.log('home route');
},
page: function () {
console.log('page route');
},
});
window.app = new AppRouter();
Backbone.history.start({pushState: true});
});
导航到www.myapp.com时会生成以下控制台输出:
> home route
> page route
当预期的控制台输出应该是:
> home route
该方法似乎违反了默认触发选项.这是实施中的错误,还是我误解了什么?
最佳答案 从6/28/12更新到最新版本的Backbone(
https://github.com/documentcloud/backbone上的主版本)可以解决问题.