Javascript 将参数传递到把手辅助对象

Javascript 将参数传递到把手辅助对象,javascript,coffeescript,underscore.js,handlebars.js,Javascript,Coffeescript,Underscore.js,Handlebars.js,所以我有一个非常简单的车把助手- Handlebars.registerHelper('selectRow', (rowIndex, selectedIds) -> console.log 'row index' console.log rowIndex console.log selectedIds isSelected = _.indexOf(rowIndex, selectedIds) if isSelected > -1 return 'clas

所以我有一个非常简单的车把助手-

Handlebars.registerHelper('selectRow', (rowIndex, selectedIds) ->
  console.log 'row index'
  console.log rowIndex
  console.log selectedIds
  isSelected = _.indexOf(rowIndex, selectedIds)
  if isSelected > -1 
    return 'class="row-selected"'
)
我有这个把手密码-

<div class="title">{{ title }}</div>
<hr/>
<table cellspacing="0" cellpadding="0">
  <thead>
    {{#each columns}}
    <th class="col-heading" data-heading="{{ this }}">{{ this }}</th>
    {{/each}}
  </thead>
  <tbody>
    {{#each rows}}
        <tr {{#selectRow @index selected }}>
          {{#each this}}
          <td>
            {{this}}
          </td>
          {{/each}}
        </tr>
      {{/selectRow}}
    {{/each}}

  </tbody>
</table>

如何将所选参数正确传递给助手

您正在把手
每个
循环中使用
选定的
变量,因此我猜想
选定的
不在范围内

我想你只是对
\uxof.indexOf
的工作原理有点困惑。从:

indexOf
索引(数组,值,[isSorted])

返回可在数组中找到值的索引,如果数组中不存在值,则返回-1

因此,您正在搜索的数组,
selectedds
,应该是第一个参数,而您正在搜索的元素是第二个参数。也许你的意思是:

isSelected = _.indexOf(selectedIds, rowIndex)
或更好(海事组织):

我通常发现,使用
\uux()
函数可以生成更清晰的代码

另外,
{{{selectRow@index selected}}
不应该是
{{selectRow@index selected}}
?前导的
#
应该引入一个块,但您的助手不是作为块助手编写的

一旦上述两个问题都得到解决,对我来说,明智的事情似乎正在发生:

isSelected = _.indexOf(selectedIds, rowIndex)
isSelected = _(selectedIds).indexOf(rowIndex)