mercredi 12 août 2015

Ember: chained requests in route:model

I want, in my route, to load several data. A user, and his videos for instance.

The URL looks like this: /user/fooUsername

But to load videos, I want to make the query using the id of the user, so I have to wait for the first promise to fulfill.

My code looks like this:

export default Ember.Route.extend({
    model: function(params) {
        var username = params.user_username;

        var user = this.store.filter('user', {filters: {"username":username}}, function(item) {
            return (item.get('username') === username);
        }).then(function(results) {
            return results.get('content')[0];
        });

        var userID = user.id;

        var videos = this.store.filter('videos', {filters: {"creator":userID}}, function(item) {
          return (item.get('creator.id') === userID);
        });


        return  Ember.RSVP.hash({user: user, videos: videos});
  }
});

Is there an easy way to do that? My attempts using afterModel, beforeModel and setupController failed.

Aucun commentaire:

Enregistrer un commentaire