Resource.query
returns an array (because of the isArray
flag you created) with two properties, $promise
which is the promise which when resolved will "copy" all the response values to the array returned from Resource.query
magically updating the view and $resolved
which is a flag telling if $promise
was resolved already, to answer your question there's actually some additional transformation happening, the data returned from your transformation will actually go through another transform (which can't be disabled) and this is where your each object is transformed into a Resource
instance.
So this is what you're expecting to happen:
promise
.then(function (rawData) {
// this is where your transformation is happening
// e.g. transformResponse is called with rawData
// you return your transformed data
})
.then(function (transformedData) {
// raw data has gone through 1 transformation
// you have to decide what to do with the data, like copying it to
// some variable for example $scope.names
})
But Resource
is doing the following:
promise
.then(function (rawData) {
// this is where your transformation is happening
})
.then(function (transformedData) {
// Resource is adding this handler and doing the
// 'copy' to array operation here for you,
// but it will actually create a Resource instance
// in the process with each item of your array!
})
.then(function (transformedDataV2) {
// raw data has gone through 2 transformations here!
})
The additional transformation is where the magic happens and is where the Resource
instance is created, if we take a look at the source code these are the lines which take care of this transformation, I'll copy them here:
if (action.isArray) {
value.length = 0;
forEach(data, function(item) {
if (typeof item === "object") {
value.push(new Resource(item));
} else {
// Valid JSON values may be string literals, and these should not be converted
// into objects. These items will not have access to the Resource prototype
// methods, but unfortunately there
value.push(item);
}
});
}
data
is the data returned by your first transformation and as seen above it'll pass the typeof item === 'Object'
check so value
which is the array returned by Resource.query
is updated with a new Resource
item (not with item). You were worried about this strange Resource
object, let's analyze the Resource
constructor:
function Resource(value) {
shallowClearAndCopy(value || {}, this);
}
It's just copying each of the properties of the object value
to this
(this
is the new Resource instance), so now we're dealing with Resource
objects and not plain array objects
will it cause an issue?
I'm sure it will, if the transform function you define is a little bit more complex like having each object actually be an instance of something else whose __proto__
has some methods, e.g. a Person
object instead of a plain object then the methods defined in Person.prototype
won't be visible to the result of the whole operation since each object won't be a Person
instance but a Resource
instance! (see this error in this plunkr, make sure to read the comments and also look at the error raised in the console because of the undefined method)