javascript - Avoiding hardcode solution to post method -


i have post method

$.post("/abc/export.action",               {                  sessiontoken: sessiontoken,                 exporttype: "xls",                 param1:comparedata,                 param2:comparepatchdata},                   function(resdata)                   {                         alert(resdata);                   }             ); 

i wanted best practice can have enumeration or array stores post parameters in , use while post, way in can avoid hardcoding of sessiontoken, param1 etc.. solution?

edit

sometimes might happen need change names of params have edit everywhere have post method, instead of if params there in enum or array easier change @ 1 place.

if want labels in object based on other variable can with

  var paramlabels = {"session": "sessiontoken", "type": "exporttype", ...}   var paramvalues = {};    paramvalues[paramlabels.session] =  sessiontoken;    paramvalues[paramlabels.type] = "xls"   ...    $.post(/"abc/export.action", paramvalues, function(resdata) { alert(resdata);}); 

i can't see benefit of approach expect when developers of backend change names of parameters every 5 minutes.

another way of handling create factory method or builder

  function createparams(session, type, ...) {      return { "sessiontoken": session, "exporttype": type, ...) }   }    var params = createparams(sessiontoken, "xls", ...); 

or

  var parameters = function() {         this.session = function(session) { this.session = session; return this;}       this.type = function(type) { this.type = type; return this;}        ...       this.build = function() {                  var params = {}             !this.session || params.sessiontoken = this.session;             !this.type || params.exporttype = this.type;             ...             return params;        }   }      var params = new parameters().session(sessiontoken).type("xls")...build(); 

both of these approaches let define concreate name of parameters once. latter may easier reuse when different set of parameters needed.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -