mardi 26 janvier 2016

Ember 2.x get model's relationships at subroute of that model

Imagine that I have two models, Author and Book.

// models/author.js
DS.Model.extend({
  name: DS.attr(),
  books: DS.hasMany('book')
});


// models/book.js
DS.Model.extend({
  title: DS.attr(),
  author: DS.belongsTo('author')
})

It would be nice to have an endpoint at /api/authors/{authorID}/books to be able to get all of the authors books in one batch request, instead of making multiple calls to /api/books/{bookID}, but it doesn't seem that ember supports this. It is possible to do /api/books?authorID={authorID}, but that would lose some of the benefits of the store.

Is there an Ember idiomatic way of doing /api/authors/{authorID}/books? Again, the goal is to be able to make one batch request to get all books for an author instead of making one call for every book in the author's hasMany list.

For a little more context, I have the following routes structure:

// router.js
...
this.route('authors', function() {
  this.route('author', { path: ':id' }, function() {
    this.route('books');
  });
});
...

In the 'authors' route I will load all of the authors. I do not want to synchronously load their related books, yet, as the list is potentially massive and not used on this route.

In the author route I'm using data of the author already retrieved (the name, in this case).

In the books route I would like to finally load all of the author's related books without needing to send a single request per book.




Aucun commentaire:

Enregistrer un commentaire