Javascript 将Backbone.js路由器与#一起使用!
当使用主干路由器时,它会将“页面”路由视为#页面 我能做点什么吗!作为默认值,而不仅仅是# 我想让HTML4浏览器使用#!(http://example.com/#!/page/subpage)和具有html5历史记录的浏览器使用普通地址,而不必使用“!page”作为路由Javascript 将Backbone.js路由器与#一起使用!,javascript,backbone.js,router,Javascript,Backbone.js,Router,当使用主干路由器时,它会将“页面”路由视为#页面 我能做点什么吗!作为默认值,而不仅仅是# 我想让HTML4浏览器使用#!(http://example.com/#!/page/subpage)和具有html5历史记录的浏览器使用普通地址,而不必使用“!page”作为路由 “#!”是使ajax页面可爬行。有关更多信息,请参阅。查看Backbone.js源代码后,它看起来像是Backbone.History模块使用的哈希标记是硬编码的 改变这一点的最简单方法是修改源本身,第741行(主干0.5.3
“#!”是使ajax页面可爬行。有关更多信息,请参阅。查看Backbone.js源代码后,它看起来像是
Backbone.History
模块使用的哈希标记是硬编码的
改变这一点的最简单方法是修改源本身,第741行(主干0.5.3):
应改为:
var hashStrip = /^#!*/;
window.location.hash = this.fragment = "!" + frag;
您还需要更改主干网中的导航
功能。历史记录
以确保出现“!”。第863行:
window.location.hash = this.fragment = frag;
需要更改为:
var hashStrip = /^#!*/;
window.location.hash = this.fragment = "!" + frag;
不幸的是,由于它是如何在Backbone.js中编写的,我不确定是否有更优雅的方法来解决这个问题。在查看Backbone.js源代码后,它看起来像是
Backbone.History
模块使用的哈希标记是硬编码的
改变这一点的最简单方法是修改源本身,第741行(主干0.5.3):
应改为:
var hashStrip = /^#!*/;
window.location.hash = this.fragment = "!" + frag;
您还需要更改主干网中的导航
功能。历史记录
以确保出现“!”。第863行:
window.location.hash = this.fragment = frag;
需要更改为:
var hashStrip = /^#!*/;
window.location.hash = this.fragment = "!" + frag;
不幸的是,由于它是如何在Backbone.js中编写的,我不确定是否有更优雅的方法来解决这个问题。您只需添加
/代码>到您的路线:
routes: {
"!/help": "help", // #!/help
"!/page/:subpage": "search", // #!/search/kiwis
"!/page/:subpage/p:page": "search" // #!/search/kiwis/p7
},
然后,您将获得完整的http://example.com/#!/页面/子页面
url。您只需添加/代码>到您的路线:
routes: {
"!/help": "help", // #!/help
"!/page/:subpage": "search", // #!/search/kiwis
"!/page/:subpage/p:page": "search" // #!/search/kiwis/p7
},
然后,您将获得完整的http://example.com/#!/页面/子页面
url.我决定使用以下方法:
//Create the Route without routes (just the functions)
App.Router = Backbone.Router.extend({
"quem-somos": function() {
alert("quem-somos");
}
});
//test for html5 history using Modernizr and instantiate the Route with normal urls for true and prefixed routes for false
if(Modernizr.history){
App.routePrefix="";
App.router = new App.Router({
routes:{
"quem-somos" : "quem-somos"
}
});
}else{
App.routePrefix="!/";
App.router = new App.Router({
routes:{
"!/quem-somos" : "quem-somos"
}
});
}
Backbone.history.start({pushState: true});
//Call navigate when clicking a button
App.router.navigate(App.routePrefix+"quem-somos",{trigger: true, replace: true});
通过这种方式,我获得了对每个浏览器的支持。我决定使用以下方法:
//Create the Route without routes (just the functions)
App.Router = Backbone.Router.extend({
"quem-somos": function() {
alert("quem-somos");
}
});
//test for html5 history using Modernizr and instantiate the Route with normal urls for true and prefixed routes for false
if(Modernizr.history){
App.routePrefix="";
App.router = new App.Router({
routes:{
"quem-somos" : "quem-somos"
}
});
}else{
App.routePrefix="!/";
App.router = new App.Router({
routes:{
"!/quem-somos" : "quem-somos"
}
});
}
Backbone.history.start({pushState: true});
//Call navigate when clicking a button
App.router.navigate(App.routePrefix+"quem-somos",{trigger: true, replace: true});
通过这种方式,我获得了每个浏览器的支持。Oops,我想我只是读了你的整个问题,意识到你不想因为推送状态而更改路由。对不起!留下这个答案,以防它会帮助其他人。它也很有效,但就像kpeel的答案一样,当使用myRoute.navigate(“页面”,true);url片段显示为#,而不是#!;嗯,是的,因为你必须通过完整的路线,即:myRoute.navigate(“!/page”,true)代码>行动!当然所以它起作用了。但对于推送状态,它会给出不希望的结果!。解决方法是使用Modernizr.history并根据测试结果给出不同的值。哎呀,我想我刚刚读了你的整个问题,意识到你不想因为推送状态而更改路由。对不起!留下这个答案,以防它会帮助其他人。它也很有效,但就像kpeel的答案一样,当使用myRoute.navigate(“页面”,true);url片段显示为#,而不是#!;嗯,是的,因为你必须通过完整的路线,即:myRoute.navigate(“!/page”,true)代码>行动!当然所以它起作用了。但对于推送状态,它会给出不希望的结果!。解决方法是使用Modernizer.history并根据测试结果给出不同的值。谢谢您的回答。现在主干识别出#!从地址,但在使用myRoute.navigate(“页面”,true)时;url片段显示为#,而不是#!;啊,好球。不幸的是,这需要对Backbone.js源代码进行另一次编辑。我会用更改更新我的答案。谢谢你的回答。现在主干识别出#!从地址,但在使用myRoute.navigate(“页面”,true)时;url片段显示为#,而不是#!;啊,好球。不幸的是,这需要对Backbone.js源代码进行另一次编辑。我将用更改更新我的答案。