Sammy.js用&amp ;,读取路线? [英] Sammy.js read route with &,?

查看:105
本文介绍了Sammy.js用&amp ;,读取路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要路线看起来像

#/routeName/?param1=aaaa&param2=bbb

添加到sammy

#/routeName/:param1/:param2

#/routeName/?:param1&:param2

没有一个工作

推荐答案

我一直在Sammy.js中查询查询参数,并发现(虽然API文档中没有提到,但Sammy确实支持查询参数。它只是添加它在路由中找到的任何查询参数到路由的Sammy.EventContext.params数组。如果您的路线结构如下:

I've been looking into query parameters myself in Sammy.js, and discovered that (although it isn't mentioned in the API docs), Sammy does support query parameters. It simply adds any query parameters that it finds in your route to the route's Sammy.EventContext.params array. If you have a route structured like this:

Sammy(function() {
    this.get("#/routeName/", function(context) {
        alert("{'param1': '" + context.params.param1 + "', 'param2': '" + context.params.param2 + "'}");
    });
});

你会发现,如果你选择路线#/ routeName /你会得到{'param1':undefined,'param2':undefined},但如果你去#/ routeName /?param1 = aaaa& param2 = bbb,你会得到{'param1':'aaaa', 'param2':'bbb'}。这允许您以许多REST API开发人员熟悉的方式处理可选参数。

You'll find that, if you go to route "#/routeName/" you'll get "{'param1': undefined, 'param2': undefined}", but if you go to "#/routeName/?param1=aaaa&param2=bbb", you'll get "{'param1': 'aaaa', 'param2': 'bbb'}". This allows you to handle optional parameters in a way that is familiar to many REST API developers.

至于为什么你的其他人两条路线不起作用:

As for why your other two routes don't work:

#/routeName/:param1/:param2

已命名,必需参数,需要至少一个非/(正斜杠)字符才能匹配路线。至少,您必须沿着#/ runRoute / a / b的路线前往,以便触发该路线的运行。如果不提供正则表达式代替命名参数,您将无法轻松获得所需效果。

has named, required parameters, which requires at least one non-/ (forward slash) character in order to match the route. At a minimum, you'd have to go to a route along the lines of "#/runRoute/a/b" in order to trigger that route from running. You wouldn't easily be able to get the effect you wanted without providing regular expressions in place of named parameters.

#/routeName/?:param1&:param2

尾随/后#/ routeName /是可选的,因为' ?'是Sammy路线中的一个特殊字符,表示前一个字符或表达式(可以使用括号定义)是可选的。您可能使用此路由匹配'#/ routeName& b'以及#/ routeName / e& b',在两种情况下param1都设置为'e',而在两种情况下param2都设置为'b'。由于Sammy中的查询参数规则,您不能轻易地在路径中定义?,但您可以使用URL转义表单'%3F'。

has the trailing / after #/routeName/ being optional due to '?' being a special character in Sammy routes that signifies the previous character or expression (which can be defined using parenthesis) is optional. You'd potentially match '#/routeName&b' as well as #/routeName/e&b' using this route, with param1 being set to 'e' in both cases and param2 being set to 'b' in both cases. Due to the query parameter rules in place within Sammy, you would not easily be able to define a '?' within a route, but you could use the URL escaped form, '%3F'.

希望这能回答你的问题,我看到了这个问题并且最近一直在研究类似的问题。我不会声称自己是Sammy最知识渊博的人,但我们在目前的工作中使用它,这是我们迄今为止能够弄清楚的。

Hopefully this answers your question, I saw this question and had been working on similar issues recently. I won't claim to be the most knowledgeable person on Sammy, but we use it at my current job and this is what we've been able to figure out about it so far.

这篇关于Sammy.js用&amp ;,读取路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆