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

Commit 26c20b7

Browse files
committed
revert: chore($resource): refactor confusing case statement
This reverts commit d50829b. This commit introduces a regression that results in urls with parameters being incorrectly generated. We need to investigate further why this is happening, for now I'm just reverting.
1 parent bdfc9c0 commit 26c20b7

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

src/ngResource/resource.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -503,33 +503,45 @@ angular.module('ngResource', ['ng']).
503503
forEach(actions, function (action, name) {
504504
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
505505

506-
Resource[name] = function () {
507-
if (arguments.length > 4) {
508-
throw $resourceMinErr('badargs',
509-
"Expected up to 4 arguments " +
510-
"[params, data, success, error], " +
511-
"got {0} arguments",
512-
arguments.length);
513-
}
514-
515-
var params, data, success, error;
516-
517-
// Find success and error callbacks
518-
for (var i = 0; i < arguments.length; i++) {
519-
if (isFunction(arguments[i])) {
520-
if (success) error = arguments[i];
521-
else success = arguments[i];
522-
arguments[i] = undefined; // reset to avoid setting data or params to a function
523-
}
524-
}
525-
526-
// Set data and params
527-
if (arguments.length <= 2 && hasBody) {
528-
data = arguments[0];
529-
} else {
530-
params = arguments[0];
531-
data = arguments[1];
506+
Resource[name] = function (a1, a2, a3, a4) {
507+
var params = {}, data, success, error;
508+
509+
/* jshint -W086 */ /* (purposefully fall through case statements) */
510+
switch (arguments.length) {
511+
case 4:
512+
error = a4;
513+
success = a3;
514+
//fallthrough
515+
case 3:
516+
case 2:
517+
if (isFunction(a2)) {
518+
if (isFunction(a1)) {
519+
success = a1;
520+
error = a2;
521+
break;
522+
}
523+
524+
success = a2;
525+
error = a3;
526+
//fallthrough
527+
} else {
528+
params = a1;
529+
data = a2;
530+
success = a3;
531+
break;
532+
}
533+
case 1:
534+
if (isFunction(a1)) success = a1;
535+
else if (hasBody) data = a1;
536+
else params = a1;
537+
break;
538+
case 0: break;
539+
default:
540+
throw $resourceMinErr('badargs',
541+
"Expected up to 4 arguments [params, data, success, error], got {0} arguments",
542+
arguments.length);
532543
}
544+
/* jshint +W086 */ /* (purposefully fall through case statements) */
533545

534546
var isInstanceCall = this instanceof Resource;
535547
var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data));

0 commit comments

Comments
 (0)