把手模板中的Escape关键字

x4shl7ld  于 2022-10-20  发布在  其他
关注(0)|答案(4)|浏览(113)

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

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

但由于“action”是一个“关键字”,它会抛出javascript运行时错误“options is undefined”。

xzabzqsa

xzabzqsa1#

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

{{this.action}}
t9eec4r0

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>​

JavaScript

Ember.CollectionView.create({
    contentBinding: 'App.controller',
    itemViewClass: Ember.View.extend({
        templateName: 'item',
        actionProp: function(){
            return this.getPath('content.action');
        }.property()
    })
}).append();
q35jwt9p

q35jwt9p3#

我真的建议你改一下房子的名字。你正在花时间解决一个不是你的领域核心的问题。
亚格尼。亲吻。
在编程中要学到的最大的教训是如何制作和完成事情,而不是如何操作javascript,使其不会对使用保留关键字的行为产生负面影响。如果你没有一个看起来很难理解的脆弱的解决方案,你会更快乐
由于这是一个浮动,我可以建议您使用“actionLevel”吗?

5ktev3wc

5ktev3wc4#

只需使用模型。属性,例如:

{{input class="input-xlarge" value=model.content }}

相关问题