[英]Accessing nested hasMany relationship in Ember / Ember Data
我正在嘗試訪問嵌套的hasMany關系,期待這個承諾。 不幸的是,我沒有得到以下任何一項的預期結果:
// Tried the following
this.get('users.emails')
this.get('users.@each.emails')
this.get('users').get('emails')
// Trying to avoid this:
this.get('users').
then(function(users) {
var arr = Ember.A();
users.forEach(function(user, index){
arr.pushObject(user.get('emails'));
});
return array;
}).
then(function(emailArr){
// then do something
});
// Preferable
this.get('users.emails').then(function(emails){
// then do something
});
如果您已將async
值設置為true定義模型,請執行以下操作:
users: DS.hasMany('user', {async: true})
然后通常調用this.get('users')
將返回一個promise數組,並且在您實際開始使用它時可能無法解析。
嵌套gets
很棘手,特別是異步模型的復雜性,所以我建議你這樣做:
this.get('users').then(function(users) {
users.get('emails').then(function(emails) {
// do stuff with emails.
});
}
惱人的? 絕對。 可能有必要嗎? 是啊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.