Last active
October 22, 2016 21:28
-
-
Save pywebdesign/a81755c46f041bed6cf1 to your computer and use it in GitHub Desktop.
Simple JsonAPi adapter for Restangular
This file contains hidden or 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
// 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; | |
}); | |
}); |
This file contains hidden or 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
//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