The arguments that I expect to receive are in the form <MethodName>?param1=value1&...¶mn=valuen
I have several methods with varying parameters that need to be called with the argument values in the correct order.
E.g. getArticle?adminId=1&customerId=2&articleId=3 will have to be mapped to method getArticle(adminId, customerId,articleId).
But also getArticle?adminId=1&articleId=3&customerId=2 will have to be mapped to method getArticle(adminId, customerId,articleId).
The values needed for the method can be taken from the last argument by getting the values for the keys.
So far so good, but I don't want the code cluttered with switch statements to explicitly map the arguments to the method parameters.
I don't want to code like
- Code: Select all
case "getArticle": scopes.api.getArticle(arguments[1]["adminId"],arguments[1]["customerId"], arguments[1]["articleId"]).
I want it made generic like
- Code: Select all
var methodParameters = scopes.api[arguments[0]].getMethodParameters();
var parameterValues = [];
for(var i=0; i<methodParameters.length; i++){
parameterValues.push(arguments[1][methodParameters[i]]);
}
scopes.api[arguments[0]](parameterValues);
So that even when the parameters are given in a different order, or getting another methodname with other parameters from the request, I don't have to alter this when extending with more methods in my api scope.
I tried to use the JSMethod method "getArguments()" (instead of my pseudo method getMethodParameters()) but that always returns null.
Is there a(nother) way to get the parameters of my scope method and call that method with the parameterValues filled with the request argument values in the right order?
Or is there some way to pass parameters to a method differently?
Of course I will add checks on the validity and completeness of the request arguments (but that is outside the scope of my question)