如何访问计算属性

时间:2012-05-21 07:07:52

标签: ember.js

我有一个Ember.Object,如:

App.HelpVideoInfo = Ember.Object.extend({
MyAccount: ['FAe8w0cUCJ0', 'How-to: Change "My Account"'],
StoreInformation: ['UddOjEG-hXw', 'How-to: Change Store Information'],

getSecondAccount:function()
{
    return this.get('MyAccount')[1];
} .property('MyAccount'),
});

我想从我的Ember.View绑定到getSecondAccount(计算属性)。我用过:

App.IconMeaningDialogView = Ember.View.extend({
       accountBinding:    'App.HelpVideoInfo.getSecondAccount';
  });

但它不起作用。感谢。

3 个答案:

答案 0 :(得分:1)

您的命名约定与Ember的命名约定不符,请参阅Emberist blog post

您应该命名类UpperCase,实例和属性lowerCase。然后绑定按预期工作,请参阅http://jsfiddle.net/pangratz666/PQYYH/

我还建议使用内置数组访问器objectAt

<强>车把

<script type="text/x-handlebars" >
    {{#view App.IconMeaningDialogView}}
        {{account}}
    {{/view}}
</script>​

<强>的JavaScript

App.helpVideoInfo = Ember.Object.create({
    myAccount: ['FAe8w0cUCJ0', 'How-to: Change "My Account"'],
    storeInformation: ['UddOjEG-hXw', 'How-to: Change Store Information'],

    secondAccount: function() {
        return this.get('myAccount').objectAt(1);
    }.property('myAccount').cacheable()
});

App.IconMeaningDialogView = Ember.View.extend({
    accountBinding: 'App.helpVideoInfo.secondAccount'
});​

答案 1 :(得分:0)

访问该属性的方法如下

App.IconMeaningDialogView = Ember.View.extend({
    accountBinding: Ember.Binding.from('App.HelpVideoInfo.getSecondAccount')
});

请参阅Advanced Emberjs Bidnings

答案 2 :(得分:0)

您可能不希望扩展 Ember.Object 来创建班级App.HelpVideoInfo。相反,如果你在那里创建一个新对象,它将允许你以你想要的方式绑定到属性。

但是,如果您想使用App.HelpVideoInfo子类,那么在绑定到其属性之前,您必须创建对象。如何执行此操作的示例:http://jsfiddle.net/74KX7/1/