我有一个页面,特定用户的 user_idhttp://www.domain.com/user/123
在哪里。123
有一个选项卡式界面,单击选项卡应使用新 URL 更新地址栏。
示例:单击<a href="likes" data-toggle="tab">Likes</a>
应将地址栏更新为http://www.domain.com/user/123/likes
问题:我正在尝试在backbone.js 中实现pushstates,但在使用.js 时似乎无法正确获取URL app.navigate()
。我尝试root
在路由器中设置属性,使用Backbone.history.start({ pushState: true, root: "/user/" });
它给我根为http://www.domain.com/user/
. 如何将 user_id 添加到末尾root
?IE。http://www.domain.com/user/123/
//Router
var AppRouter = Backbone.Router.extend({
routes: {
'': 'likes',
'likes': 'likes',
},
likes: function() {
console.log('fn likes');
$('#tab_likes"').show();
}
});
var app = new AppRouter();
Backbone.history.start({ pushState: true, root: "/user/" });
$(function() {
// Update address bar URL
$('a[data-toggle="tab"]').on('click', function(e) {
app.navigate(e.target.getAttribute('href'));
});
});
更新代码
从url获取后如何更新root
属性?下面显示的当前方法不起作用Backbone.history
user_id
// Router
var AppRouter = Backbone.Router.extend({
routes: {
':user_id/likes': 'likes',
':user_id/': 'likes'
},
set_user_id: function($user_id) {
this.user_id = $user_id;
// ADD SOME CODE TO CHANGE Backbone.history's root attribute
},
likes: function($user_id) {
console.log('fn likes');
this.set_user_id($user_id);
$('#tab_likes"').show();
}
});
var app = new AppRouter();
Backbone.history.start({
pushState: true,
root: '/user/' + app.user_id + '/' // app.user_id is undefined at the time this is called
});