2012-01-06 15 views

回答

4

终于找到了解决办法,也许有点难看

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

这并不难看,这就是[route](http://documentcloud.github.com/backbone/#Router-route)的作用,Backbone只是将路由表翻译为内部路由。 – 2012-01-06 18:49:37

3

我相信这会为你工作:

'/对象/:ID/*图示'

你的 '身份证'参数仍然会匹配你的id,最后的'splat'将匹配最后附加的任何内容。它甚至一无所获。所以如果你想在没有任何获取参数的情况下触发这条路线,那么'/ object/1337 /'将起作用。注意最后的斜线。它必须在那里。

你的原始链接/ object/1337?var =/hey也应该触发这条路线。

编辑:您可以在

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

编辑编辑阅读提示图标:您的原始链接将与新的斜杠您的帐号和工作之间的“?”

/对象/ 1337/VAR = /哎

+0

它匹配,但ID等于1337?var = /嘿 – Intrepidd 2012-01-06 14:47:09

+0

是检查我的'编辑编辑'。你需要在'1337 /?'之间的斜线为了让你所有的参数都包含在摔跤图中。 – Mike 2012-01-06 14:52:12

+0

所以无论如何。无论你是否得到params,你都会在你的id后面加上'/'。 – Mike 2012-01-06 14:53:11

0

我用下面这个:

// 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); 
0

如果你想要的是从片段中分离参数,我建议使用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' } 

} 
相关问题