使用把手迭代 javascript 对象
我正在尝试向 Handlebars 注册助手,以允许迭代 JSON 对象。 这个要点看起来是一个合适的解决方案。我将其转换为以下 CoffeeScript。当我使用任何一个帮助程序时,似乎什么都没有发生(这对于普通 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 应用程序中。
I am trying to register helpers with Handlebars to allow iterating over JSON objects. This gist looked like an appropriate solution. I converted that into the following CoffeeScript. Nothing seems to happen when I use either of the helpers (that holds true for both vanilla JavaScript and the CoffeeScript version). Any ideas?
$ ->
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
In the template:
{{#key_value categories}}
I'M ALIVE!!
{{/key_value}}
{{#each_with_key categories key="category_id"}}
I'M ALIVE!!
{{/each_with_key}}
I am currently using gem 'handlebars-assets'
in the Gemfile to add handlebars to a rails app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 JavaScript 到 CoffeeScript 的音译已损坏。您不使用
for ... in
来迭代 CoffeeScript 中的对象,而是使用for k, v ...
:这个 CoffeeScript 循环:
变成这个 JavaScript:
所以如果
y
是一个没有length
属性的对象,那么_len
将是undefined 和 JavaScript
for(;;)
循环根本不会迭代。您还应该使用
own
而不是hasOwnProperty
:但这更多是为了方便而不是正确性。
另外,CoffeeScript 循环是表达式,因此您通常会说 array = expr for own k, v in o 或等效形式:
if
expr
超过一行或太多很长以便于可读理解。CoffeeScript 中助手的正确且更惯用的版本看起来更像是这样的:
演示: http://jsfiddle.net/ambigously/LWTPv/
Your JavaScript to CoffeeScript transliteration is broken. You don't use
for ... in
to iterate over an object in CoffeeScript, you usefor k, v of ...
:This CoffeeScript loop:
becomes this JavaScript:
So if
y
is an object without alength
property, then_len
will beundefined
and the JavaScriptfor(;;)
loop won't iterate at all.You should also be using
own
instead ofhasOwnProperty
:but that's more for convenience than correctness.
Also, CoffeeScript loops are expressions so you'd usually say
array = expr for own k, v in o
or the equivalent form:if
expr
is more than one line or too long to allow for a readable comprehension.A correct and more idiomatic version of your helpers in CoffeeScript would look more like this:
Demo: http://jsfiddle.net/ambiguous/LWTPv/