Just say I have a relationship like so:
App.Library = DS.Model.extend
authors = DS.hasMany(App.Author, {async: true})
App.Author = DS.Model.extend
books: DS.hasMany(App.Book, {async: true)
App.Book = DS.Model.extend()
and I want to get all the books from all the authors in my library. I could do a simple flatten style operation if these were not Promises
How do I do similar when both relationships are async.
Here is what I have come up with
allBooks: Ember.computed('authors', function() {
var r = []
this.get('authors').then(function(authors) {
authors.forEach(function(author) {
author.get('books').then(function(books) {
books.forEach(function(book){
r.push.apply(r, book);
});
});
});
});
return r;
}
Update:
I should have stated this earlier, but I actually did have this working with the following code. But now that ArrayController is deprecated I am looking for other ways to achieve this same result.
allBooks: Ember.computed('authors', function() {
var allBooks = Ember.ArrayController.create();
this.get('authors').then(function(authors) {
authors.forEach(function(author) {
author.get('books').then(function(books) {
books.forEach(function(book){
allBooks.addObject(book);
});
});
});
});
return allBooks;
}
I suppose I could do something like this is there another way that is better?:
books: Ember.computed('authors', function() {
var a = Ember.Object.create({
content: Ember.A()
});
this.get('authors').then(function(authors) {
authors.forEach(function(author) {
author.get('books').then(function(books) {
books.forEach(function(book){
a.get('content').addObject(book);
});
});
});
});
return a.get('content');
}),