2013-04-11 57 views
7

我試圖創建一個計算屬性,讓我得到所有頁面長度的總和。EmberJS:無法獲得兩個級別的hasMany數組的長度

但我不知道如何訪問一個孩子,所以我可以得到那個孩子的孩子。

App.Document = DS.Model.extend({ 
    name: DS.attr('string'), 
    spreads: DS.hasMany('App.Spread'), 

    pagesCount: function() { 
       // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length. 
       var spreadsLength = this.get('spreads.length'); 
       var firstSpread = this.get('spreads')[0]; 
       return firstSpread.get('pages.length'); 
    }.property('spreads') 
}); 

App.Spread = DS.Model.extend({ 
    document: DS.belongsTo('App.Document'), 
    pages: DS.hasMany('App.Page') 
}) 

App.Page = DS.Model.extend({ 
    spread: DS.belongsTo('App.Spread'), 
    page_name: DS.attr('string'), 
    page_items: DS.hasMany('DS.PageItem') 
}) 

回答

11

這裏是你如何傳播的陣列中可以訪問的第一個對象的例如:

App.Document = DS.Model.extend({ 
    name: DS.attr('string'), 
    spreads: DS.hasMany('App.Spread'), 

    pagesCount: function() { 
     // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length. 
     var spreadsLength = this.get('spreads.length'); 

     var firstSpread = this.get('spreads').objectAt(0); 
     // var firstSpread = this.get('spreads.firstObject'); // elegant way to first Object 

     return firstSpread.get('pages.length'); 
    }.property('spreads.firstObject.pages.length') 
}); 

,但我想你想在這裏獲得的總頁數。 所以,這裏是一個例子,如何進行迭代的利差和總結的的頁數:

App.Document = DS.Model.extend({ 
    name: DS.attr('string'), 
    spreads: DS.hasMany('App.Spread'), 

    pagesCount: function() { 
     // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length. 
     var spreadsLength = this.get('spreads.length'); 
     var ret = 0; 
     this.get("spreads").forEach(function(spread)){ 
      ret += spread.get('pages.length'); 
     } 
     return ret; 
    }.property('[email protected]') 
}); 

注:看屬性依賴我通過property聲明。由於ComputedProperty依賴於這些路徑,因此您需要在那裏聲明它們。

+0

謝謝,工作就像一個魅力! – Snidd 2013-04-15 17:05:03

+0

非常感謝你這篇文章。 – 2014-07-01 20:20:38

+0

如何訪問模板中的長度? – sunoceansand 2015-06-16 04:24:16

-3
App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('st App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    phone: DS.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { 
    return fields.firstName && fields.lastNameenter code here 
    } 

});ring'), 
    phone: D App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    phone: DS.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    phone: DS.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    phone: DS.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { 
    return fields.firstName && fields.lastNameenter code here 
    } 

}); { 
    return fields.firstName && fields.lastNameenter code here 
    } 

}); 
    return fields.firstName && fields.lastNameenter code here 
    } 

});S.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { 
    return fields.firstName && fields.lastNameenter code here 
    } 

}); 

App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    phone: DS.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { 
    return fields.firstName && fields.lastNameenter code here 
    } 

}); 
    phone: DS.attr('string'), 
    status: DS.attr(' App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    phone: DS.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { 
    return fields.firstName && field App.User = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    email: DS.attr('string'), 
    phone: DS.attr('string'), 
    status: DS.attr('string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { 
    return fields.firstName && fields.lastNameenter code here 
    } 

});s.lastNameenter code here 
    } 

});string', { defaultValue: 'new' }), 
    notes: DS.attr('string'), 
// projects: DS.hasMany("project", {async: true}), 
    projectsCount: function() { 
// alert(this.get('projects')); 
     return this.get('projects.length'); 
    }.property('id'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName') 
    }.property('firstName', 'lastName') 

}), 

App.User.reopenClass({ 

    valid: function(fields) { 
    return fields.firstName && fields.lastNameenter code here 
    } 

}); 
+2

只是粘貼代碼沒有幫助。請考慮添加一些解釋! – Pascal 2014-08-05 11:51:06

+0

@帕斯卡爾 - 所以倒下了它。不要舉報。這是一個有效的答案,所以不值得刪除。 – ArtOfWarfare 2014-10-23 18:59:02

+0

@ArtOfWarfare相反:如果它沒有幫助,它只是佔用空間(更不用說降薪的懲罰,這是不值得的) – 2014-10-23 20:14:54