2

我有一个对象集合(MyApp.instrsController,一个 ArrayController),每个对象都有一个名为“action”(浮点数)的属性,如何使用车把模板显示它?

以下是我尝试过的代码:

<ul>
{{#each MyApp.instrsController}}
   <li>{{action}}</li>
{{/each}}
</ul>

但由于“动作”是一个“关键字”,它会引发 javascript 运行时错误“选项未定义”。

先谢谢了。

4

4 回答 4

2

您可以通过在属性名称前加上 来手动指定您正在当前上下文中查找属性this.

{{this.action}}
于 2012-05-30T02:34:17.623 回答
1

If you can't change the property name you can use a computed property in your model object, see http://jsfiddle.net/pangratz666/4ZQM8/:

Handlebars:

<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
        }));
    }
});

If you don't have a custom model object, you can use a computed property on a CollectionView, see http://jsfiddle.net/pangratz666/r6XAc/:

Handlebars:

<script type="text/x-handlebars" data-template-name="item" >
    {{actionProp}}
</script>​

JavaScript:

Ember.CollectionView.create({
    contentBinding: 'App.controller',
    itemViewClass: Ember.View.extend({
        templateName: 'item',
        actionProp: function(){
            return this.getPath('content.action');
        }.property()
    })
}).append();
于 2012-05-30T09:34:15.547 回答
1

我严重建议您更改属性的名称。您正在花时间处理一个不是您的领域核心的问题。

雅尼。吻。

在编程中要学习的最大教训是如何完成工作,而不是如何操纵 javascript 以免对您使用保留关键字的行为嗤之以鼻。如果您没有看起来难以理解的脆弱解决方案,您以后会更快乐

由于这是一个浮点数,我可以建议您使用“actionLevel”吗?

于 2012-05-30T13:26:38.887 回答
-2

只需使用model.property,例如:

{{input class="input-xlarge" value=model.content }}
于 2014-04-05T14:05:31.143 回答