Created
June 22, 2015 20:45
-
-
Save Dhaulagiri/cf5787663d9f93e99d05 to your computer and use it in GitHub Desktop.
Ember Data 2.0 JSONAPISerializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Partially working...