2013-04-11 13 views
7

iki düzeyli bir hasMany dizisinin uzunluğunu alamıyorum Tüm sayfaların uzunluğunun toplamını almak için hesaplanmış bir özellik oluşturmaya çalışıyorum.EmberJS:

Ama bir çocuğa nasıl erişeceğimi anlayamıyorum, böylece o çocuğun çocuğunu alabilirim.

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') 
}) 

cevap

11

Burada yayılır dizideki ilk nesneye erişmek nasıl bir örnektir:

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') 
}); 

Ama burada toplam sayfa numarası almak istiyorum sanırım.

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]') 
}); 

Not: i property aracılığıyla bildirilen özelliği bağımlılığına bak Yani, burada sayfaların sayısını yayılır yineleme ve toplamak için nasıl bir örnektir. ComputedProperty bu yollara bağlı olduğundan, onları orada bildirmeniz gerekir.

+0

Teşekkürler, bir çekicilik gibi çalıştı! – Snidd

+0

Bu yazı için çok teşekkür ederim. –

+0

Şablondaki uzunluğa nasıl erişirsiniz? – sunoceansand

-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

sadece kod yapıştırma yardımcı olmuyor. Lütfen biraz açıklama yapmayı düşünün! – Pascal

+0

@Pascal - Yani onu reddet. İşaretleme. Bu bir cevapta geçerli bir girişimdir, bu yüzden silinmeye değmez. – ArtOfWarfare

+0

@ArtOfWarfare tam tersi: eğer yardım etmezse, sadece yer kaplıyor (bu not düşmemek için cezaya değinmiyor, buna değmez) –

İlgili konular