5

我在 Rails 3.2 jquery 移动应用程序中使用 Handlebar。

我正在尝试在 Coffeescript 方法中编写 switch case 语句,例如

Handlebars.registerHelper 'status', (blog) ->
  switch(parseInt(blog.status))
    when [0..20]
      status = "active"
    when [20..40]
      status = "Moderately Active"
    when [40..60]
      status = "Very Active"
    when [60..100]
      status = "Hyper Active"
  return status

我没有得到任何结果。如何在 when 中使用范围。请建议

4

2 回答 2

16

switch不会像评论中的Cygal注释那样工作(即参见issue 1383)。Aswitch只是一个美化的if(a == b)结构,您需要能够说出以下内容:

a = [1,2,3]
switch a
...

当你switch在一个阵列上时让它工作。CoffeeScript 设计者认为添加一个(脆弱的)特殊情况来处理数组(就是这样[a..b])特别不值得。

您可以使用if

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  if 0 <= status <= 20
    'Active'
  else if 20 < status <= 40
    'Moderately Active'
  else if 40 < status <= 60
    'Very Active'
  else if 60 < status <= 100
    'Hyper Active'
  else
    # You need to figure out what to say here

或者像这样短路return

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  return 'Something...'      if status <=   0
  return 'Active'            if status <=  20
  return 'Moderately Active' if status <=  40
  return 'Very Active'       if status <=  60
  return 'Hyper Active'      if status <= 100
  return 'Something else'    # This return isn't necessary but I like the symmetry

请注意,您需要为以下三种特殊情况添加字符串:

  1. status < 0.
  2. status > 100.
  3. statusNaN。这种情况通常属于最终的“它不小于或等于 100”分支,因为NaN => n并且NaN <= n对于 all 都是错误的n

是的,您绝对确定状态将始终在假设范围内。另一方面,不可能的事情总是发生在软件中(因此 comp.risks 邮件列表)并且没有充分的理由留下如此容易填补的漏洞。

还要注意在parseInt调用中添加了 radix 参数,你不希望前导零把事情弄得一团糟。是的,基数参数是可选的,但它确实不应该是,你的手指应该自动添加, 10到你所做的每个parseInt调用中。

于 2012-06-26T07:47:08.257 回答
8

在 mu 中添加一点点是太短的答案,您可以将其第二个代码片段转换为switch表达式:

Handlebars.registerHelper 'status', (blog) ->
  status = parseInt(blog.status, 10)
  switch
    when status <= 0   then 'Something...'      
    when status <= 20  then 'Active'
    when status <= 40  then 'Moderately Active'
    when status <= 60  then 'Very Active'
    when status <= 100 then 'Hyper Active'
    else 'Something else'

这基本上相当于switch (true)在 JavaScript 中执行 a (尽管 CS 编译器将生成一个switch (false)带有否定条件的语句以确保表达式的布尔结果......我认为)。


switch超出范围不起作用的原因是 CS 中的范围文字表示普通的旧 JS 数组(尽管编译器在执行类似的操作时会做一些优化技巧for i in [1..something]),所以当它们在 a 中被发现时,它们会switch被视为正常数组值

// Generated JS for question's CS code:
switch (parseInt(blog.status)) {
  case [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]:
    status = "active";
    break;
  case [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]:
    status = "Moderately Active";
    break;
  case [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]:
    status = "Very Active";
    break;
  case (function() {
      _results = [];
      for (_i = 60; _i <= 100; _i++){ _results.push(_i); }
      return _results;
    }).apply(this):
    status = "Hyper Active";
}

语句中的值switch基本上与 using 的每个case值进行比较===,它仅适用于基元,不适用于数组(即使它适用于数组,它也会测试数组相等性,而不是switched 值是否包含在caseed 数组中) .

于 2012-06-26T09:37:19.240 回答