在 Ember.js #each 中生成循环计数器 [英] Generating a loop counter in Ember.js #each

查看:17
本文介绍了在 Ember.js #each 中生成循环计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 Ember.js 中车把模板的限制非常严重.你不能执行任意的 javascript.您所获得的只是一组固定的能力(if/else、each、action、view、outlet)以及在当前上下文对象上输出属性值的能力.

I'm finding the restrictions of handlebar templates in Ember.js to be absolutely crippling. You can't execute arbitrary javascript. All you get is a fixed set of abilities (if/else, each, action, view, outlet) and the ability to output the value of a property on the current context object.

这意味着我无法弄清楚如何做一些像使用循环计数器这样简单的事情,例如,根据它在我正在迭代的集合中的位置,在循环内的 html 元素上设置位置 ID.

Which means I cannot figure out how to do something as simple as utilizing a loop counter in order to, for example, slap positional IDs on an html element inside a loop based on it's position in the collection I'm iterating on.

将其添加为模型的计算属性是没有意义的,因为这不是模型级知识(同一模型可能位于不同视图中的两个不同位置).

It doesn't make sense to add it as a computed property on the model, because that's not model-level knowledge (the same model could be in two different positions in different views).

仅供参考 - 在 PostgreSQL 上使用 Ember-1.0-pre、Ember-Data 和 Rails 与 ActiveModel::Serializers.

FYI-Using Ember-1.0-pre, Ember-Data, and Rails with ActiveModel::Serializers on PostgreSQL.

推荐答案

我建议编写一个把手帮助程序,您可以在其中执行任意 javascript.看http://handlebarsjs.com 一些示例.

I'd recommend writing a handlebars helper, in which you can execute arbitrary javascript. See http://handlebarsjs.com for some examples.

或者,您可以在子视图(而不是模型)中包含位置 ID.

Alternatively, you could include the positional IDs on your child views (not your models).

这篇关于在 Ember.js #each 中生成循环计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆