angular/angular.js — pull request #2992 by ledbettj (2013-06-17 18:43:01+00:00)
Title: Support extracting resources from wrapped responses and storing metadata.

This PR adds a `dataKey` option to the `actions` hash passed to `$resource`.

When a dataKey is provided, the resources are extracted from the specified key on the response object. This is basically the same as providing:

transformRequest: function(data, headers) {
return angular.fromJson(data)[dataKey];
}

However, this PR also adds the following functionality: when a dataKey is provided, and `isArray == true`, any properties on the response object besides the dataKey itself will be copied over to the collection returned. For example:

$resource('/api/items/:id', {id: '@id'}, {
query: {
isArray: true,
dataKey: 'items'
}
}

/* suppose the server responds with: */
{
page_size: 3,
page: 1,
total_items: 1900,
items: [ { ... }, { ...} , {... } ]
}

/* in our controller */
$scope.items = Items.query();
/* we can now also access: */
$scope.items.total_items; // 1900
$scope.items.page; // 1
$scope.page_size; // 3

I've tried to adhere to the contribution guidelines as best I can. Love to get some feedback or direction on this. This addresses #1318, and perhaps #412 (although that one seems vaguer to me).