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

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

问题描述

我发现Ember.js中的句柄模板的限制是绝对瘫痪的。你不能执行任意javascript。所有你得到的是一套固定的能力(如果/其他,每个,动作,视图,插座),并且能够输出当前上下文对象的属性值。



这意味着我无法弄清楚如何使用循环计数器来做一些简单的事情,例如,根据循环中的一个集合中的位置,在循环中的一个html元素上拍打位置ID。



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



FYI-在PostgreSQL上使用Ember-1.0-pre,Ember-Data和Rails与ActiveModel :: Serializers。

解决方案

我建议您编写一个handlebars帮助程序,您可以在其中执行任意的javascript。有关例子,请参阅
http://handlebarsjs.com



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


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.

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).

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

解决方案

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

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

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

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