8000 refactor +addCustomOperation+ to eliminate condition · sumit-jaiswal/restangular@9f09939 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 9f09939

Browse files
committed
refactor +addCustomOperation+ to eliminate condition
1 parent 01297fe commit 9f09939

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/restangular.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -924,20 +924,16 @@ restangular.provider('Restangular', function() {
924924

925925
function addCustomOperation(elem) {
926926
elem[config.restangularFields.customOperation] = _.bind(customFunction, elem);
927-
_.each(['put', 'patch', 'post', 'get', 'delete'], function(oper) {
927+
var requestMethods = { get: customFunction, delete: customFunction };
928+
_.each(['put', 'patch', 'post'], function(name) {
929+
requestMethods[name] = function(operation, elem, path, params, headers) {
930+
return _.bind(customFunction, this)(operation, path, params, headers, elem);
931+
};
932+
});
933+
_.each(requestMethods, function(requestFunc, name) {
934+
var callOperation = name === 'delete' ? 'remove' : name;
928935
_.each(['do', 'custom'], function(alias) {
929-
var callOperation = oper === 'delete' ? 'remove' : oper;
930-
var name = alias + oper.toUpperCase();
931-
var callFunction;
932-
933-
if (/^(post|put|patch)$/.test(oper)) {
934-
callFunction = function(operation, elem, path, params, headers) {
935-
return _.bind(customFunction, this)(operation, path, params, headers, elem);
936-
};
937-
} else {
938-
callFunction = customFunction;
939-
}
940-
elem[name] = _.bind(callFunction, elem, callOperation);
936+
elem[alias + name.toUpperCase()] = _.bind(requestFunc, elem, callOperation);
941937
});
942938
});
943939
elem[config.restangularFields.customGETLIST] = _.bind(fetchFunction, elem);

0 commit comments

Comments
 (0)
0