我有一个对象集合(MyApp.instrsController,一个ArrayController),每个对象都有一个名为“action”(浮点数)的属性,如何使用车把模板显示它?下面是我尝试过的代码:
MyApp.instrsController
ArrayController
<ul> {{#each MyApp.instrsController}} <li>{{action}}</li> {{/each}} </ul>
但由于“action”是一个“关键字”,它会抛出javascript运行时错误“options is undefined”。
xzabzqsa1#
您可以通过在属性名称前面加上this.来手动指定正在当前上下文中查找属性:
this.
{{this.action}}
t9eec4r02#
如果无法更改特性名称,则可以在模型对象中使用计算特性,请参见http://jsfiddle.net/pangratz666/4ZQM8/:
把手:
<script type="text/x-handlebars" > <ul> {{#each App.controller}} <li>{{actionProp}}</li> {{/each}} </ul> </script>
JavaScript:
App.Object = Ember.Object.extend({ actionProp: function() { return this.get('action'); }.property('action') }); App.controller = Ember.ArrayController.create({ content: [], addObj: function(number) { this.pushObject(App.Object.create({ action: number })); } });
如果没有自定义模型对象,可以在CollectionView上使用计算属性,请参见http://jsfiddle.net/pangratz666/r6XAc/:
<script type="text/x-handlebars" data-template-name="item" > {{actionProp}} </script>
Ember.CollectionView.create({ contentBinding: 'App.controller', itemViewClass: Ember.View.extend({ templateName: 'item', actionProp: function(){ return this.getPath('content.action'); }.property() }) }).append();
q35jwt9p3#
我真的建议你改一下房子的名字。你正在花时间解决一个不是你的领域核心的问题。亚格尼。亲吻。在编程中要学到的最大的教训是如何制作和完成事情,而不是如何操作javascript,使其不会对使用保留关键字的行为产生负面影响。如果你没有一个看起来很难理解的脆弱的解决方案,你会更快乐由于这是一个浮动,我可以建议您使用“actionLevel”吗?
5ktev3wc4#
只需使用模型。属性,例如:
{{input class="input-xlarge" value=model.content }}
4条答案
按热度按时间xzabzqsa1#
您可以通过在属性名称前面加上
this.
来手动指定正在当前上下文中查找属性:t9eec4r02#
如果无法更改特性名称,则可以在模型对象中使用计算特性,请参见http://jsfiddle.net/pangratz666/4ZQM8/:
把手:
JavaScript:
如果没有自定义模型对象,可以在CollectionView上使用计算属性,请参见http://jsfiddle.net/pangratz666/r6XAc/:
把手:
JavaScript:
q35jwt9p3#
我真的建议你改一下房子的名字。你正在花时间解决一个不是你的领域核心的问题。
亚格尼。亲吻。
在编程中要学到的最大的教训是如何制作和完成事情,而不是如何操作javascript,使其不会对使用保留关键字的行为产生负面影响。如果你没有一个看起来很难理解的脆弱的解决方案,你会更快乐
由于这是一个浮动,我可以建议您使用“actionLevel”吗?
5ktev3wc4#
只需使用模型。属性,例如: