Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.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
Backbone.js 在主干路由中将url作为哈希映射传递?_Backbone.js_Backbone Routing - Fatal编程技术网

Backbone.js 在主干路由中将url作为哈希映射传递?

Backbone.js 在主干路由中将url作为哈希映射传递?,backbone.js,backbone-routing,Backbone.js,Backbone Routing,我试图将目录位置和文件路径作为骨干路由中哈希映射的一部分进行传递。这是包含哈希映射的url: localhost/index.html#目录-http://localhost/foobar-html/foo.html 这是我的路线映射上述url的内容: routes: { 'directory-*directoryPath-*filePath': 'render' }, render: function (directoryPath, filePath) { // I get

我试图将目录位置和文件路径作为骨干路由中哈希映射的一部分进行传递。这是包含哈希映射的url:

localhost/index.html#目录-http://localhost/foobar-html/foo.html
这是我的路线映射上述url的内容:

routes: {
    'directory-*directoryPath-*filePath': 'render'
},

render: function (directoryPath, filePath) {
    // I get the entire url in directoryPath variable
    // http://localhost/foobar-html/foo.html
    // and filePath is empty.
}
映射此类散列URL的正确方法是什么?谢谢

来自:

路由可以包含参数部分,
:param
,在斜杠之间匹配单个URL组件;和splat部分
*splat
,可以匹配任意数量的URL组件

您的问题是,一个splat会吃掉所有东西,因此在一条路线上有两个splat是没有意义的;您不能使用参数部分,
:x
,因为它在斜线处停止

你可以做几件事

  • 您可以对链接中的斜杠进行URI编码,并使用参数部分。URL如下所示:

    routes: {
        'directory-:directoryPath-:filePath': 'render'
    },
    render: function(d, f) {
        d = decodeURIComponent(d);
        f = decodeURIComponent(f);
        //...
    }
    
    initialize: function() {
        this.route(/directory-(.*?)-(.*)/, 'render');
    },
    render: function(d, f) {
        //...
    }
    
    #目录http:%2f%2flocalhost%2foobar html%2foo.html
    
    路由器是这样的:

    routes: {
        'directory-:directoryPath-:filePath': 'render'
    },
    render: function(d, f) {
        d = decodeURIComponent(d);
        f = decodeURIComponent(f);
        //...
    }
    
    initialize: function() {
        this.route(/directory-(.*?)-(.*)/, 'render');
    },
    render: function(d, f) {
        //...
    }
    
    演示:

  • 您可以使用将路由添加为正则表达式,这将使您在构建模式时有更多的自由。例如,像这样的片段:

    routes: {
        'directory-:directoryPath-:filePath': 'render'
    },
    render: function(d, f) {
        d = decodeURIComponent(d);
        f = decodeURIComponent(f);
        //...
    }
    
    initialize: function() {
        this.route(/directory-(.*?)-(.*)/, 'render');
    },
    render: function(d, f) {
        //...
    }
    
    #目录-http://localhost/foobar-html/foo.html
    
    可以使用如下路由器处理:

    routes: {
        'directory-:directoryPath-:filePath': 'render'
    },
    render: function(d, f) {
        d = decodeURIComponent(d);
        f = decodeURIComponent(f);
        //...
    }
    
    initialize: function() {
        this.route(/directory-(.*?)-(.*)/, 'render');
    },
    render: function(d, f) {
        //...
    }
    
    演示:

  • 第二个选项将遇到问题,因为在
    目录路径
    文件路径
    中不可避免地会出现
    -
    ;您可以对嵌入的
    -
    进行URI编码,以使它们通过第一个选项