Building a News Feed with Ember Data


#1

So I’m somewhat new to Ember and I’m looking to build a sort of social network app. I’ve managed to get Ember Data syncing up with my API (written to conform to JSON-API). Looking up records and handling relationships all works great. But now I’ve gotten to building the News Feed section and I’m stuck.

What would be the “Ember Way” to fetch a user’s news feed, and populate it with - say - Post models in Ember Data? I can only seem to query Ember Data to get all Posts, which is no good for me. I need to call an API route that has a personalised list of Posts, potentially not in sequential order. Do I need to create a new model? I’ve got no idea where to start.

Any help would be greatly appreciated!


#2

Hi benking. I don’t think you need to create a new model.

An API endpoint to get all posts may be /api/posts. How does your “personalised list of posts” endpoint look like?

Based on that information we’ll find the right approach for you to build the feed.


#3

Well it doesn’t exist yet, I’m building the API alongside the Ember app. I imagine the feed would probably be either /api/users/1/feed or as a shortcut /api/me/feed


#4

A specific subset of models is usually retrieved through query:

model() {
  return this.store.query('post', { userId: user.id });
}

In your post adapter (i.e. app/adapters/post.js) override the urlForQuery function telling it to use a specific URL whenever the userId parameter is supplied:

urlForQuery(query) {
  if (query.userId) {
    return `${this.urlPrefix()}/users/${query.userId}/feed`;
  }
  return this._super(...arguments);
}