Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple JsonAPi adapter for Restangular
// it require either lodash or underscorejs
.config(function(RestangularProvider) {
// add a response interceptor
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
extractedData = data.data;
extractedData.meta = data.meta;
extractedData.included = data.included;
function _apply(elem, fct){
if(elem !== undefined){
if(elem.type !== undefined){
fct(elem);
}else{
_.forEach(elem, function(el){
_apply(el, fct);
});
}
}
}
_apply(data.data, function(elem){
_apply(elem.relationships, function(rel){
rel.getIncluded = function(){
return _.find(extractedData.included, function(included){
a = included.type == rel.type;
b = included.id == rel.id;
return a && b;
});
};
});
});
return extractedData;
});
});
//simply call getIncluded on a relationship object and bam!
seller.relationships.user.data.getIncluded().attributes.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.