Fed03
Fed03

Reputation: 585

What to pass to ember-data if no records are found on server?

The question is simple.

I've to retrieve a list of messages from the backend with ember, but there's the possibility that a specific user has none.

What response expects ember-data in theese situations? And what's the best way to handle them?

Thanke you

Upvotes: 0

Views: 130

Answers (2)

tstirrat
tstirrat

Reputation: 1056

Lets say we have a user with id 1234, we might do a HTTP GET on /api/users/1234/messages. If there were no messages for this user, ember-data would expect a payload like so:

{ "messages": [] }

Note that the top level (root) key of the payload is the plural of the Model's typeKey according to Ember Data. It is expecting a plural because we are asking for an Array of messages.

The typeKey is essentially a camelCase of the model's class name. If the model is App.SuperVillain, the typeKey will be superVillain. If you were asking the back end for a collection of SuperVillains, the root key would be superVillains.

{ "superVillains": [] }

Upvotes: 1

claptimes
claptimes

Reputation: 1717

You can just return an empty object {}. Ember data should be fine with this.

Upvotes: 0

Related Questions