我正在尝试使用Handlebars注册帮助程序以允许迭代JSON对象. This gist看起来是一个合适的解决方案.我将其转换为以下CoffeeScript.当我使用任何一个帮助程序时,似乎没有任何事情发生(对于vanilla JavaScript和CoffeeScript版本都适用).有任何想法吗?
$->
Handlebars.registerHelper "key_value", (obj, fn)->
buffer = ""
key
for key in obj
if obj.hasOwnProperty(key)
buffer += fn({key: key, value: obj[key]})
buffer
Handlebars.registerHelper "each_with_key", (obj, fn)->
context
buffer = ""
key
keyName = fn.hash.key
for key in obj
if obj.hasOwnProperty(key)
context = obj[key]
if keyName
context[keyName] = key
buffer += fn(context)
buffer
在模板中:
{{#key_value categories}}
I'M ALIVE!!
{{/key_value}}
{{#each_with_key categories key="category_id"}}
I'M ALIVE!!
{{/each_with_key}}
我目前正在Gemfile中使用gem’handlebars-assets’来将一个把手添加到rails应用程序中.
解决方法:
您的JavaScript到CoffeeScript音译被破坏了.您不使用for … in迭代CoffeeScript中的对象,您使用for k, v of ...
:
Use
of
to signal comprehension over the properties of an object instead of the values in an array.
这个CoffeeScript循环:
for x in y
...
成为这个JavaScript:
for (_i = 0, _len = y.length; _i < _len; _i++) {
x = a[_i];
...
}
因此,如果y是没有length属性的对象,那么_len将是未定义的,并且(;;)循环的JavaScript将不会迭代.
您还应该使用own而不是hasOwnProperty:
If you would like to iterate over just the keys that are defined on the object itself, by adding a
hasOwnProperty
check to avoid properties that may be interited from the prototype, usefor own key, value of object
.
但这更方便而不是正确.
另外,CoffeeScript循环是表达式,所以你通常会说自己的数组为exp = exp,v为o或等价形式:
array = for own k, v in o
expr
如果expr超过一行或太长而不允许可读的理解.
CoffeeScript中正确且更惯用的助手版本看起来更像是:
Handlebars.registerHelper "key_value", (obj, fn)->
(fn(key: key, value: value) for own key, value of obj).join('')
Handlebars.registerHelper "each_with_key", (obj, fn)->
key_name = fn.hash.key
buffer = for own key, value of obj
value[key_name] = key
fn(value)
buffer.join('')
演示:http://jsfiddle.net/ambiguous/LWTPv/