Created
December 27, 2013 01:14
-
-
Save samuraisam/8141094 to your computer and use it in GitHub Desktop.
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
function extractEmbeddedData(obj, propertyName, store, persistedName) { | |
var ids = []; | |
var embeddedObjs = []; | |
if (!Ember.isNone(obj[propertyName])) { | |
var subObj = obj[propertyName]; | |
var isArray = Array.isArray(subObj); | |
if (!isArray) { | |
subObj = [subObj]; | |
} | |
subObj.forEach(function(subElement) { | |
embeddedObjs.push(subElement); | |
ids.push(subElement.id); | |
if (!Ember.isNone(store) && !Ember.isNone(persistedName)) { | |
store.push(persistedName, subElement); | |
} | |
}); | |
obj[propertyName] = isArray ? ids : ids[0]; | |
} | |
return embeddedObjs; | |
} | |
App.MerchantSerializer = DS.DjangoRESTSerializer.extend({ | |
extractSingle: function(store, type, payload) { | |
extractEmbeddedData(payload, 'locations', store, 'location'); | |
return this._super.apply(this, arguments); | |
}, | |
extractArray: function(store, type, payload) { | |
var locations = []; | |
payload.forEach(function(merchant) { | |
extractEmbeddedData(merchant, 'locations', store, 'location'); | |
}); | |
return this._super.apply(this, arguments); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment