I have two REST APIs I need to use: One from Mongolab for development purposes and one from the actual API that is not accessible at the moment. The problem is that the ID is handled a bit differently in these and the object structure differs. Mongo uses the object._id.$oid
notation and the actual API object.ID
notation. The Mongolab resource is:
app.factory('Items', function ($resource) {
var items = $resource('https://api.mongolab.com/api/1/databases/x/collections/items/:id',
{
apiKey:'x',
id:'@_id.$oid'
}
});
return items;
});
And the query call (currently using):
$scope.items = Items.query({}, function () {
if (API == 'Mongo') {
angular.forEach($scope.items, function(item) {
item.ID = item._id.$oid;
});
};
});
I want to be able to easily switch the different APIs without modifying the code in every query call or link (I have dozens of calls and links with resource IDs). So I want to move the API == 'Mongo'
check to upper level: I tried to use the forEach
ID altering directly in the factory where I create the Items
resource but it doesn't work that way. How can I modify the results directly before the results are populated through query
?
Or should I just create different branches for different APIs?