Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Backbone.js路由器与#一起使用!_Javascript_Backbone.js_Router - Fatal编程技术网

Javascript 将Backbone.js路由器与#一起使用!

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

当使用主干路由器时,它会将“页面”路由视为#页面

我能做点什么吗!作为默认值,而不仅仅是#

我想让HTML4浏览器使用#!(http://example.com/#!/page/subpage)和具有html5历史记录的浏览器使用普通地址,而不必使用“!page”作为路由


“#!”是使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源代码进行另一次编辑。我将用更改更新我的答案。