我在Ember遇到一个奇怪的问题,我真的需要一种解决方法
我有以下内容:
{{#unbound each items}}
<span>{{unbound myValue}}</span>
{{unbound view App.MyView content=this.subItems}}
{{/unbound}}
哪个应该输出以下内容:
<div>
<span>First item</span>
<div>
...first other view...
</div>
<div>
...nth other view...
</div>
</div>
...
<div>
<span>nth item</span>
<div>
...first other view...
</div>
<div>
...nth other view...
</div>
</div>
有两件事要注意:
现在,这是问题所在:我使用了上面的“#unbound each”助手,从理论上讲,它应该输出上面的DOM结构。 代替我仍然从每个块中获取变形标记:
<script id="metamorph-0-start"></script>
<script id="metamorph-1-start"></script>
<div>
<span>First item</span>
<div>
...first other view...
</div>
<div>
...nth other view...
</div>
</div>
<script id="metamorph-1-end"></script>
<script id="metamorph-0-end"></script>
...
<script id="metamorph-n0-start"></script>
<script id="metamorph-n1-start"></script>
<div>
<span>nth item</span>
<div>
...first other view...
</div>
<div>
...nth other view...
</div>
</div>
<script id="metamorph-n1-end"></script>
<script id="metamorph-n0-end"></script>
我在这里做错了什么?
这是现场JS Fiddle演示的问题:
http://jsfiddle.net/NQKvy/942/
最佳答案
{{unbound}}
帮助程序不能用作块帮助程序,并且不能与{{each}}
一起使用
您总是需要在特定的属性上使用它,例如:
{{unbound myProperty}}
请参阅github上讨论此问题的线程:https://github.com/emberjs/ember.js/pull/1457
好消息是,在HTMLBars出来之前(我最后看到的是它们正在为Ember 1.8拍摄),您可以使用
{{group}}
帮助程序。它不是ember核心的一部分,但我已经在Ember 1.5中成功使用了它,它将为您摆脱掉变形(以及数据绑定(bind))。在这里下载:
最简单的方法是将https://github.com/emberjs/group-helper/blob/master/packages/group-helper/lib/main.js的内容复制到您自己的帮助器中。
然后,您可以执行以下操作:
{{#group}}
{{#each items}}
<span>{{unbound myValue}}</span>
{{view App.MyView content=this.subItems}}
{{/each}}
{{/group}}
注意:正如罗伯特(Robert)在下面的注释中所述,
{{#group}}
帮助器也将影响 View 本身。希望有帮助!