Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

chore($resource): Use shallow copy instead of angular.copy #5300

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ function lookupDottedPath(obj, path) {
return obj;
}

/**
* Create a shallow copy of an object and clear other fields from the destination
*/
function shallowClearAndCopy(src, dst) {
dst = dst || {};

angular.forEach(dst, function(value, key){
delete dst[key];
});

for (var key in src) {
if (src.hasOwnProperty(key) && key.substr(0, 2) !== '$$') {
dst[key] = src[key];
}
}

return dst;
}

/**
* @ngdoc overview
* @name ngResource
Expand Down Expand Up @@ -393,7 +412,7 @@ angular.module('ngResource', ['ng']).
}

function Resource(value){
copy(value || {}, this);
shallowClearAndCopy(value || {}, this);
}

forEach(actions, function(action, name) {
Expand Down Expand Up @@ -465,7 +484,7 @@ angular.module('ngResource', ['ng']).
if (data) {
// Need to convert action.isArray to boolean in case it is undefined
// jshint -W018
if ( angular.isArray(data) !== (!!action.isArray) ) {
if (angular.isArray(data) !== (!!action.isArray)) {
throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
'response to contain an {0} but got an {1}',
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
Expand All @@ -477,7 +496,7 @@ angular.module('ngResource', ['ng']).
value.push(new Resource(item));
});
} else {
copy(data, value);
shallowClearAndCopy(data, value);
value.$promise = promise;
}
}
Expand Down