简体   繁体   English

自定义Backbonejs路由参数

[英]Custom Backbonejs route parameters

Consider this URL: 考虑以下URL:

domains.google.com/registrar#t=b domains.google.com/registrar#t=b

note: 注意:

#t=b #t = b

In this example, the variable "t" stores the current tab on the page where "b" is for billing. 在此示例中,变量“ t”将当前标签存储在页面上,其中“ b”用于计费。


How can I achieve query like parameters in backbone as shown above? 如上所示,如何在主干中实现类似参数的查询?

I understand Backbone has routes that support parameters in urls, but this is limited to when the data is in a hierarchy, for example: item/:id 我知道Backbone的路由支持url中的参数,但这仅限于数据处于层次结构中时,例如:item /:id

But what about application settings that would not work well in a directory like structure? 但是,在像目录这样的目录中无法正常工作的应用程序设置呢?

The only solution I can think of is a custom parser and break up the key/values myself. 我能想到的唯一解决方案是自定义解析器,然后自己分解键/值。

Any ideas? 有任何想法吗?

Expanding on @try-catch-finally 's comment, I'm going to show you how to create your own route with a simple RegEx pattern that will match your conditions. 扩展@try-catch-finally的注释,我将向您展示如何使用简单的RegEx模式创建适合您条件的自己的路线。

Here's the regex we'll use: 这是我们将使用的正则表达式:

^\w+?           # match one word (or at least a character) at the 
                # beginning of the route

[=]             # until we parse a single 'equals' sign

(               # then start saving the match inside the parenthesis

[a-zA-Z0-9]*    # which is any combination of letters and numbers

)               # and stop saving

Putting it all together the regex looks like: /^\\w+?[=]([a-zA-Z0-9]*)/ . 将所有正则表达式放在一起看起来像: /^\\w+?[=]([a-zA-Z0-9]*)/

Now we set up our router, 现在我们设置路由器

var MyRouter = Backbone.Router.extend({
  initialize: function(options) {
    // Matches t=b, passing "b" to this.open
    this.route(/^\w+?(?<=[=])(.*)/, "testFn");
  },

  routes: {
      // Optional additional routes
  },

  testFn: function (id) {
      console.log('id: ' + id );
  }
});

var router = new MyRouter();
Backbone.history.start();

The TL;DR is that in the MyRouter.initialize we added a route that takes the regex above and invokes the MyRouter.testFn function. TL; DR是在MyRouter.initialize添加的路由,该路由采用上述正则表达式并调用MyRouter.testFn函数。 Any call to h ttp://yourdomain.com#word1=word2 will call the MyRouter.testFn with the word after the parenthesis as a parameter. 任何对h ttp://yourdomain.com#word1=word2调用都将使用括号后的word作为参数来调用MyRouter.testFn Of course, your word place setting could be a single character like in your question t=b . 当然,您的word放置设置可以是单个字符,例如问题t=b

Expanding your parameters 扩展参数

Say you want to pull multiple parameters, not just the one. 假设您要提取多个参数,而不仅仅是一个。 The key to understanding how Backbone pulls your parameters is the capturing group () . 了解Backbone如何提取参数的关键是捕获组 () A capturing group allows your to "save" the match defined within the parenthesis into variables local to the regex expression. 捕获组允许您将括号中定义的匹配“保存”到正则表达式的局部变量中。 Backbone uses these saved matches as the parameters it applies to the the route callback. 骨干网将这些保存的匹配项用作应用于路由回调的参数。

So if you want to capture two parameters in your route you'd use this regex: 因此,如果要在路径中捕获两个参数,则可以使用此正则表达式:

^\w+?[=]([a-zA-Z0-9]*)[,]\w+?[=]([a-zA-Z0-9]*)

which simply says to expect a comma delimiter between the two parameter placeholders. 它只是说要在两个参数占位符之间使用逗号定界符。 It would match, 会匹配的,

t=b,some=thing

More general route patterns 更一般的路线模式

You can repeat the [,]\\w+?[=]([a-zA-Z0-9]*) pattern as many times as you need. 您可以根据需要多次重复[,]\\w+?[=]([a-zA-Z0-9]*)模式。 If you want to generalize the pattern, you cold use the non-capturing token (?: ... ) and do something like, 如果要概括模式,可以冷使用非捕获令牌(?: ... )然后执行类似的操作,

^\w+?[=]([a-zA-Z0-9]*)(?:[,]\w+?[=]([a-zA-Z0-9]*))?(?:[,]\w+?[=]([a-zA-Z0-9]*))?

The regex above will look for at least one match and will optionally take two more matches. 上面的正则表达式将查找至少一个匹配项,并且可以选择再进行两个匹配项。 By placing a ? 通过放置一个? token at the end of the (?: ... ) group, we say the pattern in the parenthesis may be found zero or one times (ie it may or may not be there). (?: ... )组末尾的标记中,我们说括号中的模式可能被发现零次或一次(即它可能存在或可能不存在)。 This allows you to set a route when you know you can expect up to 3 parameters, but sometimes you may want only one or two. 当您知道可以期望最多3个参数时,这使您可以设置一条路线,但是有时您可能只需要一个或两个。

Is there a truly general route? 有真正的一般路线吗?

You may be asking yourself, why not simply use one greedy (?: ... ) group that will allow an unlimited number of matches. 您可能会问自己,为什么不简单使用一个贪婪(?: ... )组,该组将允许无限数量的匹配。 Something like, 就像是,

^\w+?[=]([a-zA-Z0-9]*)(?:[,]\w+?[=]([a-zA-Z0-9]*))*

With this regex pattern you must supply one parameter, but you can take an unlimited number of subsequent matches. 使用此正则表达式模式,您必须提供一个参数,但是您可以接受无限数量的后续匹配项。 Unfortunately, while the regex will work fine, you won't get the desired result. 不幸的是,尽管正则表达式可以正常工作,但您将无法获得理想的结果。 (See, for example, this Question .) (例如,请参阅此Question 。)

That's a limitation of JavaScript. 那是JavaScript的局限性。 With repeating capturing-groups (ie the ([a-zA-Z0-9]*) capturing-group will repeat with every repetition of the (?: ... ) non-capturing-group ) JavaScript only saves the last match. 使用重复的捕获组 (即([a-zA-Z0-9]*) 捕获组将与(?: ... ) 非捕获组的每次重复都重复一次,JavaScript仅保存最后一个匹配项。 So if you pass a route like t=b,z=d,g=f,w=1ee , you'll only save 1ee . 因此,如果您通过类似t=b,z=d,g=f,w=1ee ,则只会保存1ee So, unfortunately you have to have an idea of what the maximum number of parameters your route should take, and manually code them into your regex pattern like we did above. 因此,不幸的是,您必须了解路由应采用的最大参数数量,然后像上面一样手动将它们编码为您的regex模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM