是否可以使用backbone创建一条匹配所有以某某开头的url的路由?
我正在尝试使用与 '/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
终于找到解决办法了,可能有点难看
Finally found a solution, maybe a little ugly
我相信这对您有用:
'/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
我为此使用以下内容:
I use following for this:
如果您想要的只是将参数与片段分开,我建议使用 backbone-query-params。
您可以使用常见的 Backbone 路由,插件将解析对象中的 GET 参数,然后将其传递给路由处理函数。
从插件的 Github 页面:
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: