Skip to content

Instantly share code, notes, and snippets.

@pywebdesign
Last active October 22, 2016 21:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pywebdesign/a81755c46f041bed6cf1 to your computer and use it in GitHub Desktop.
Save pywebdesign/a81755c46f041bed6cf1 to your computer and use it in GitHub Desktop.
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