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

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
677f10b
chore($resource): Use shallow copy instead of angular.copy
kseamon Dec 3, 2013
4797ba7
Reverts http.js
kseamon Dec 4, 2013
e210f91
Removes a white space, per review.
kseamon Dec 4, 2013
fbc5cf5
docs(tutorial/step-12): fix refernce to incorrect jquery version
elwinarens Nov 26, 2013
280b5ce
chore(closure): add `$routeProvider#redirectTo` function parameters
Nov 27, 2013
1e7675a
docs(input): remove deprecated isolated scope pitfall
revolunet Nov 27, 2013
b38a228
docs(tutorial/step-3): add module to `ng-app` directive in code sample
programistka Nov 28, 2013
e8f4305
docs($interpolate): demonstrate a filter in the interpolated expression
esgy Nov 28, 2013
d802ed1
fix($rootScope): broadcast $destroy event on $rootScope
jeffbcross Dec 4, 2013
93901bd
fix($animate): ensure ms durations are properly rounded
matsko Nov 28, 2013
b6d5439
fix(input): ensure ngModelWatch() triggers second digest pass when ap…
Dec 5, 2013
21e48ab
chore(travis): move checks from before_scripts to scripts
vojtajina Dec 5, 2013
0e50810
fix(ngInit): evaluate ngInit before ngInclude
Nov 30, 2013
958d3d5
fix($animate): ensure animations work with directives that share a tr…
matsko Dec 4, 2013
04a570d
docs(TRIAGING): Initial doc about triaging issues in Angular
tbosch Dec 5, 2013
39c5ffb
docs(tutorial/step-2): remember to install karma plugins
petebacondarwin Dec 5, 2013
2adbcf1
docs(tutorial/step-3): remember to install karma plugins
petebacondarwin Dec 5, 2013
09648e4
docs(tutorial/step-6): remove unused `class="diagram"`
mjomble Nov 29, 2013
1722574
chore($resource): Use shallow copy instead of angular.copy
kseamon Dec 3, 2013
89c146d
Merge branch 'shallow-copy' of https://github.com/kseamon/angular.js …
kseamon Dec 5, 2013
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
6 changes: 3 additions & 3 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,9 @@ function $HttpProvider() {
} else {
// serving from cache
if (isArray(cachedResp)) {
resolvePromise(cachedResp[1], cachedResp[0], copy(cachedResp[2]));
resolvePromise(copy(cachedResp[1]), cachedResp[0], copy(cachedResp[2]));
} else {
resolvePromise(cachedResp, 200, {});
resolvePromise(copy(cachedResp), 200, {});
}
}
} else {
Expand All @@ -980,7 +980,7 @@ function $HttpProvider() {
function done(status, response, headersString) {
if (cache) {
if (isSuccess(status)) {
cache.put(url, [status, response, parseHeaders(headersString)]);
cache.put(url, [status, copy(response), parseHeaders(headersString)]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the changes in this file should be a separate commit. I like this change but it is not related to the changes to in $resource

} else {
// remove promise from the cache
cache.remove(url);
Expand Down
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) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also remove the trailing space while you are at it :)

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