While considering ricick's suggestion, it is important to note that implementing a method on the array may present some challenges. Essentially, creating a wrapper around $resource and its instances is necessary for achieving this functionality. The issue lies within a specific line of code in angular-resource.js:
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
This code snippet sets up the return value from $resource. During the execution of an ajax request, "value" is populated and returned. After the completion of the request, the value is assigned to "value", but by reference using the angular.copy() method. Each element of the array (such as query()) will be an instance of the resource being operated on.
To extend this functionality, one approach could involve modifying the following code (untested snippet which might require adjustments):
var myModule = angular.module('myModule', ['ngResource']);
myModule.factory('Book', function($resource) {
var service = $resource('/authors/:authorId/books'),
origQuery = service.prototype.$query;
service.prototype.$query = function (a1, a2, a3) {
var returnData = origQuery.call(this, a1, a2, a3);
returnData.myCustomMethod = function () {
// Implement custom method logic here...
return returnData;
}
}
return service;
});
Admittedly, further tweaking may be required to make it fully functional, but this serves as a basic outline of the concept.