是否可以使用backbone创建一条匹配所有以某某开头的url的路由?

发布于 2024-12-25 10:01:57 字数 188 浏览 0 评论 0原文

我正在尝试使用与 '/object/:id' 匹配的backbone.js 进行路由

问题是,我可以接收包含任何内容(包括斜杠)的获取参数,然后backbone 无法识别此网址:/object/1337?var =/hey

我可以忽略获取参数或者只是说我希望我的路线以“/object/:id”开头吗? ?

谢谢。

I'm trying to do a route with backbone.js that match '/object/:id'

Problem is, i can receive get parameters containing anything, including slashes, then backbone doesn't recognize this url : /object/1337?var=/hey

Can i ignore get parameters or simply say that i want my route to begin with '/object/:id?' ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不甘平庸 2025-01-01 10:01:57

终于找到解决办法了,可能有点难看

initialize : function() {
  this.route(/^object\/([^\?]*)\?/, "show", this.show);
}

Finally found a solution, maybe a little ugly

initialize : function() {
  this.route(/^object\/([^\?]*)\?/, "show", this.show);
}
梦断已成空 2025-01-01 10:01:57

我相信这对您有用:

'/object/:id/*splat'

您的 'id' 参数仍将与您的 id 匹配,并且末尾的 'splat' 将与末尾附加的任何内容匹配。它甚至不会匹配任何内容。因此,如果您想在没有任何 get 参数的情况下触发此路由,则“/object/1337/”将起作用。注意末尾的斜杠。它必须在那里。

您的原始链接 /object/1337?var=/hey 也应该触发此路由。

编辑:您可以在

http://documentcloud.github.com/backbone/#Router 阅读有关 splats 的信息-routes

编辑编辑:您的原始链接将与您的 id 和“?”之间的新斜杠一起使用

/object/1337/?var=/嘿

I believe this would work for you:

'/object/:id/*splat'

Your 'id' parameter will still match your id and the 'splat' on the end will match anything that is appended at the end. It will even match nothing. So if you want to trigger this route without any get parameters then '/object/1337/' will work. Notice the slash at the end. It has to be there.

Your original link of /object/1337?var=/hey should also trigger this route.

EDIT: You can read about splats at

http://documentcloud.github.com/backbone/#Router-routes

EDIT EDIT: Your original link will work with the new slash in between your id and the '?'

/object/1337/?var=/hey

剑心龙吟 2025-01-01 10:01:57

我为此使用以下内容:

// Call for Backbone to make the route regexp
var route = Backbone.Router._routeToRegExp(route_string);
// Change the regexp to ignore GET parameters
route = new RegExp(route.source.replace(/\$/, "\\\/?(?:(\\\?.*$))*$");
// Create a route with a new regexp
// Backbone will not try to make a regexp from the route argument if it's already a RegExp object
this.route(route, name, callback);

I use following for this:

// Call for Backbone to make the route regexp
var route = Backbone.Router._routeToRegExp(route_string);
// Change the regexp to ignore GET parameters
route = new RegExp(route.source.replace(/\$/, "\\\/?(?:(\\\?.*$))*$");
// Create a route with a new regexp
// Backbone will not try to make a regexp from the route argument if it's already a RegExp object
this.route(route, name, callback);
指尖微凉心微凉 2025-01-01 10:01:57

如果您想要的只是将参数与片段分开,我建议使用 backbone-query-params

您可以使用常见的 Backbone 路由,插件将解析对象中的 GET 参数,然后将其传递给路由处理函数。

从插件的 Github 页面:

routes: {
    'object/:id': 'myRoute'
}
...
myRoute: function(id, params) {

    // if the routed url is '/object/1337?var=/hey'
    console.log( id );     // ---> '1337'
    console.log( param );  // ---> { var : '/hey' }

}

If all you want is to separate parameters from the fragment, I suggest using backbone-query-params.

You can use common Backbone routes and the plugin will parse the GET parameters in an object that is then passed to the route handler function.

From the plugin's Github page:

routes: {
    'object/:id': 'myRoute'
}
...
myRoute: function(id, params) {

    // if the routed url is '/object/1337?var=/hey'
    console.log( id );     // ---> '1337'
    console.log( param );  // ---> { var : '/hey' }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文