Nicorr
Nicorr

Reputation: 734

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

In this configuration, userID is null.

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

EDIT: I am still facing this problem, out of ways to solve it. What should I do?

Upvotes: 0

Views: 85

Answers (1)

ChrisBarthol
ChrisBarthol

Reputation: 4959

I think you want to first find the User and then find his videos. It might look something like this

store.find("user", params.id).then(function(result) {
  var promises;
  promises = {
    user: result,
    videos: store.findQuery("video", {
      video_id: result.get('id')
    })
  };
  return Ember.RSVP.hash(promises);
});

Here we are finding a user by id. Once we find that user we go out and find their videos and return them.

Upvotes: 1

Related Questions