Backbone.js 在主干路由中将url作为哈希映射传递?
我试图将目录位置和文件路径作为骨干路由中哈希映射的一部分进行传递。这是包含哈希映射的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
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
,因为它在斜线处停止
你可以做几件事
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编码,以使它们通过第一个选项