Calculated property based on children in parent collection

I have a jsbin to illustrate my problem: http://emberjs.jsbin.com/xuperezacuha/1/

I have a controller with a property that is a collection of objects (parents) and each parent has a collection of objects (children). For example, a collection of books in which each book contains a collection of chapters.

From jsbin: I have a computed property in my pointer controller named allChapters

, it is not computed when a new chapter is added to either parent.

How do I base a computed property on a child object being added to any of the parent's child collections?

+3


source to share


1 answer


Create an ember object with a computed property and wrap it in that object.

App.Book = Em.Object.extend({
  chapterLength: Em.computed.alias('chapters.length')
});


App.IndexRoute = Ember.Route.extend({
  model: function() {
    return [
      App.Book.create({name: 'red', chapters: []}),
      App.Book.create({name: 'blue', chapters: []}),
      App.Book.create({name: 'yellow', chapters: []})
    ];
  }
});

      

Then view the computed property



 allChapters: function() {
    _ret = []
    this.get('model').forEach(function(book) {
      book.get('chapters').forEach(function(chapter) {
         _ret.pushObject(chapter);
      })
    })
    return _ret;
  }.property('model.@each.chapterLength'),

      

http://jsbin.com/xuperezacuha/4/edit

+3


source







All Articles