Configuring Ember-Data to sideload relationships

When you first start using Ember-Data its pretty nifty to see your application making JSON requests to create/read/update your entities but at some point you might want to cut down on the number of requests that your Ember app is making to the server. One technique for doing this is to sideload your associations.

So if you’ve got a User entity which has many opinons, you can include the opinions in the /users/<id> response, instead of simply returning the user with ids for the opinions which would cause your application to create new requests for each of the opinions.

First you need to configure your RESTAdapter to expect these opinions in sideloaded form:

Your Ember-data User model should look like:

Now, if you have a Rails back-end and you’re using the Active Model Serializers gem, you can simply modify the UserSerializer to include the Opinions in the User response.

assuming that you have a User Model with a has_many association to the Opinion model.

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s