Saturday, 31 August 2013

Optimization of code in javascripts JSON calls

Optimization of code in javascripts JSON calls

I've been involved in a large web application where I have a lot of
functions that calls web services through JSON. For instance:
/*...*/
refreshClientBoxes: function(customerNr) {
var request = {};
request.method = "getClientBoxes";
request.params = {};
request.params.customerNr = customerNr;
request.id = Math.floor(Math.random() * 101);
postObject(jsonURL, JSON.stringify(request), successClientBoxes);
},
/*...*/
Where "postObject" it's a function that receive an URL, the data and a
callback.
As you can see I have to construct this piece of code in every single method:
var request = {};
request.method = "getClientBoxes";
request.params = {};
request.params.customerNr = customerNr;
request.id = Math.floor(Math.random() * 101);
What's change is the name of the method that we will call and the name and
values of parameter that we want to pass.
So I was wondering if there is a way that we can avoid this effort through
a method that receive the name of the method that we will call and array
of parameters, and using some kind of reflection construct the request
parameters and return the request stringifyed.
For the WS I used php + zend 1.12, the MVC framework in JS its ember 0.95
and jQuery.

No comments:

Post a Comment