为数组内的属性创建别名属性

时间:2018-04-05 09:00:38

标签: ember.js

考虑以下数组

siteid address
123    1 New Street, New Town, Newvile
456    2 Elm Road, New York, New York

我无法为数组内的属性添加别名,如下所述

auth: [{
    username: "",
    password: "",
  },
  {
    username: "",
    password: "",
  }
]

如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

您可以使用firstObject返回第一个元素
objectAt()获取数组的特定元素 我担心后者不适用于computed.alias()

  auth: [{
    username: "firstName",
    password: "",
  },
  {
    username: "maxmustermann",
    password: "",
  }
  ],

  defaultUsername: Ember.computed.alias('auth.firstObject.username'),
  username: Ember.computed('auth', function() { 
                   return this.get('auth').objectAt(1).username; 
            }),