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; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment