Skip to content

Instantly share code, notes, and snippets.

@Dhaulagiri
Created June 22, 2015 20:45
Show Gist options
  • Save Dhaulagiri/cf5787663d9f93e99d05 to your computer and use it in GitHub Desktop.
Save Dhaulagiri/cf5787663d9f93e99d05 to your computer and use it in GitHub Desktop.
Ember Data 2.0 JSONAPISerializer
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONAPISerializer.extend({
normalizeArrayResponse: function(store, primaryModelClass, payload, id, requestType) {
var data = {},
extracted = [],
root = 'data',
type = Ember.String.pluralize(primaryModelClass.modelName);
payload[type].forEach(function(e) {
e.attributes = [];
// iterate through the object and push any attributes into the attributes array
Ember.keys(e).forEach(function(key) {
// but don't push id or attributes itself into the array
if (key !== 'id' && key !== 'attributes') {
e.attributes[key] = e[key];
delete e[key];
}
});
// set the type
e.type = type;
extracted.push(e);
});
data[root] = extracted;
return this._super(store, primaryModelClass, data, id, requestType);
}
});
@Dhaulagiri
Copy link
Author

Partially working...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment