Backbone.js的路由器的困惑(pushState的:真正的,最后的斜线) [英] Backbone.js Routers confusion (pushState: true, trailing slash)
问题描述
我使用的骨干与 pushState的路由器:真正的
来处理我的网站的网址。 URL的例子包括:
I am using Backbone's router with pushState:true
to handle my site's urls. Examples of URLs include:
- http://domain.com/John
- http://domain.com/John/
- http://domain.com/John/photos
- http://domain.com/John/my-latest-photos-2012
- http://domain.com/John
- http://domain.com/John/
- http://domain.com/John/photos
- http://domain.com/John/my-latest-photos-2012
问题:当用户进入 http://domain.com/John/ ,预期功能照片
被执行。然而,当用户进入 http://domain.com/John 没有斜线,没有任何反应;我的猜测是,在根定义尾部的反斜杠
prevented这一点。
Problem: When the user goes to http://domain.com/John/, the expected function photos
is executed. However when the user goes to http://domain.com/John without the trailing slash, nothing happens; my guess is that the trailing backslash defined in root
prevented this.
路由器
var AppRouter = Backbone.Router.extend({
routes: {
'': 'photos',
'photos': 'photos'
},
viewing_username: $('#viewing_username').val(), // eg: 'John'
photos: function() {
console.log('photos');
}
});
var app = new AppRouter();
Backbone.history.start({
pushState: true,
root: '/' + app.viewing_username + '/'
});
的jQuery
$('a[data-toggle="tab"]').on('click', function(e) {
app.navigate(e.target.getAttribute('href'), true);
});
第二尝试
问题:这个时候我删除了根尾部的反斜杠
和 http://domain.com/John 现在触发路由。问题此时正值当用户在 http://domain.com/John (我相信被处理由浏览器,所以当链路(与属性数据切换=标签
命名页面约翰
) )被点击,网址更改为 http://domain.com/Johnphotos 没有分隔条件 /
。
2nd Attempt
Problem:: This time I removed the trailing backslash in root
and http://domain.com/John now triggers the route. The problem this time comes when the user is at http://domain.com/John (which I believe is treated by the browser as a page named John
), so when the link (with attribute data-toggle="tab"
) is clicked, the url is changed to http://domain.com/Johnphotos without the seperating /
.
我应该怎么解决这个问题呢?
How should I solve this problem?
推荐答案
我想,如果你更新骨干到最新版本的第2次尝试应该工作。看到这个讨论:
I think your 2nd attempt should work if you update backbone to the latest version. See this discussion:
https://github.com/documentcloud/backbone/pull/1505
上面的变化进行了合并17天以前。
The changes above were merged 8 days ago.
这篇关于Backbone.js的路由器的困惑(pushState的:真正的,最后的斜线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!