开发者

Avoiding hardcode solution to post method

开发者 https://www.devze.com 2023-03-22 08:17 出处:网络
I have this post method $.post(\"/abc/export.action\", { sessiontoken: sessiontoken, 开发者_如何转开发exportType: \"xls\",

I have this post method

$.post("/abc/export.action",
              { 
                sessiontoken: sessiontoken,
           开发者_如何转开发     exportType: "xls",
                param1:compareData,
                param2:comparePatchData},
                  function(resdata)
                  {
                        alert(resdata);
                  }
            );

I wanted some best practice so that I can have enumeration or array which stores all my post parameters in it and use it while post, A way in which I can avoid hardcoding of sessiontoken, param1 etc.. what could be the solution?

EDIT

Sometimes it might happen that I need to change the names of params so i have to edit everywhere I have post method, instead of that if all the params where there in some enum or array it would be much easier to just change at one place.


If you want the labels in a object to be based on some other variable you can do it 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 really see the benefit of this approach expect when the developers of the backend like to change the names of the parameters every five minutes.

Another way of handling this would be to create a factory method or a 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 you define the concreate name of the parameters only once. The latter may be easier to reuse when different set of parameters are needed.


Instead of passing an object literal to $.post() you can pass an object that was defined elsewhere in your code, and thus re-use that same object for multiple posts. Or put in a function call that returns an object with the appropriate parameters set up.

var postParams = {
  sessiontoken: sessiontoken,
  exportType: "xls",
  param1:compareData,
  param2:comparePatchData
};

$.post("/abc/export.action",
       postParams,
       function(resdata) { alert(resdata); });

$.post("/abc/import.action",
       postParams,
       function(resdata) { alert(resdata); });

// or use a function that can return a different object
// depending on some conditions (or parameters passed to
// the function though I haven't shown that)
function getPostParams() {
  if (someCondition)
    return postParams;
  else
    return {
      sessionToken : something,
      // etc.
    };
}

$.post("/abc/someother.action",
       getPostParams(),
       function(resdata) { alert(resdata); });
0

精彩评论

暂无评论...
验证码 换一张
取 消