Grapho
Grapho

Reputation: 1654

Ember-Data: How to get properties from nested JSON

I am getting JSON returned in this format:

{
  "status": "success",
  "data": {
    "debtor": {
      "debtor_id": 1301,
      "key": value,
      "key": value,
      "key": value
    }
  }
}

Somehow, my RESTAdapter needs to provide my debtor model properties from "debtor" section of the JSON.

Currently, I am getting a successful call back from the server, but a console error saying that Ember cannot find a model for "status". I can't find in the Ember Model Guide how to deal with JSON that is nested like this?

So far, I have been able to do a few simple things like extending the RESTSerializer to accept "debtor_id" as the primaryKey, and also remove the pluralization of the GET URL request... but I can't find any clear guide to reach a deeply nested JSON property.

Extending the problem detail for clarity:

I need to somehow alter the default behavior of the Adapter/Serializer, because this JSON convention is being used for many purposes other than my Ember app.


My solution thus far:

With a friend we were able to dissect the "extract API" (thanks @lame_coder for pointing me to it)

we came up with a way to extend the serializer on a case-by-case basis, but not sure if it really an "Ember Approved" solution...

// app/serializers/debtor.js
export default DS.RESTSerializer.extend({
    primaryKey: "debtor_id",
    extract: function(store, type, payload, id, requestType) {
        payload.data.debtor.id = payload.data.debtor.debtor_id;
        return payload.data.debtor;
    }
});

It seems that even though I was able to change my primaryKey for requesting data, Ember was still trying to use a hard coded ID to identify the correct record (rather than the debtor_id that I had set). So we just overwrote the extract method to force Ember to look for the correct primary key that I wanted.

Again, this works for me currently, but I have yet to see if this change will cause any problems moving forward....

I would still be looking for a different solution that might be more stable/reusable/future-proof/etc, if anyone has any insights?

Upvotes: 0

Views: 621

Answers (2)

bfcoder
bfcoder

Reputation: 3132

The json that ember is expecting needs to look like this:

"debtor": {
  "id": 1301,
  "key": value,
  "key": value,
  "key": value
}

It sees the status as a model that it needs to load data for. The next problem is it needs to have "id" in there and not "debtor_id".

If you need to return several objects you would do this:

"debtors": [{
  "id": 1301,
  "key": value,
  "key": value,
  "key": value
},{
"id": 1302,
  "key": value,
  "key": value,
  "key": value
}]

Make sense?

Upvotes: 1

lame_coder
lame_coder

Reputation: 3115

From description of the problem it looks like that your model definition and JSON structure is not matching. You need to make it exactly same in order to get it mapped correctly by Serializer.

If you decide to change your REST API return statement would be something like, (I am using mock data)

//your Get method on service
public object Get()
{
    return new {debtor= new { debtor_id=1301,key1=value1,key2=value2}};
}

Upvotes: 2

Related Questions