jsonruby-on-rails-4ember.jsember-datarocketpants

Adapting Sutto's RocketPants REST api with Ember.js


Ember.js expects a json response to contain the model object in the root of the json. RocketPants encodes it's models on an attribute called response with the objects as it's value. Also the metadata returned from RocketPants is in the root next to the response object while Ember expects all metadata to reside in an object named meta. As a result any of the metadata returned by RocketPants will be parsed as an object and an error will occur in Ember. It will also try to parse the response object as a response model.

So currently I am trying to decide whether to change the json sent back from the backend or writing an adapter on the ember side. Writing an adapter is the ideal approach but I don't know how easy it is to accomplish this and am currently researching it.

If I do decide to change the backend I want to keep RocketPants for it's error handling and just send back my custom json but then the same problem may arise because the json returned uses an error object and Ember might try to parse that as an object unless it takes into account the status code and it understands it's an error.

Does anyone have any incite on this topic?


Solution

  • If the root key has only one type of records in it you can use the JSONSerializer and overwrite normalizePayload (https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L190) to move the records from the response key to the top level.

    You can override extractMeta (https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L947) to move the metadata to the meta key and call this._super(store, type, payload) or to just extract the metadata yourself.