Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Refactor code to use rest call instead of action attribute in Dynamic Form

    Hi All,

    I have the following code in which I need to add an authorization token as http header. This is a legacy code so i am not sure how to refactor it so that I can add the required header.
    Can you please provide me with suggestions?


    addFileNoSmartClientServer: function () { var self = this, iFrame, fileUploadForm, uploadWindow, onSuccess_c; token = alu.nms.CpbRestApi.getAuthorizationToken(); console.log("The token value: ", token); iFrame = document.getElementById("alu_nms_cpb_upload_iframe"); onSuccess_c = function () { alu.nms.EBMessage.create({ topic_s: alu.nms.CpbApp.CPB_CHANNEL, type_i: alu.nms.CpbApp.REFRESH_SYSTEMS_LIST_EVENT, originator_o: self, payload_o: null }).send(); }; iFrame.onload = function iFrameOnLoad() { var retJsonStr, retJson; retJsonStr = iFrame.contentDocument.body.innerHTML; if (retJsonStr) { // Some browsers add <pre> tags around the returned data // Chrome likes to place the 'style' attribute in the <pre> also... retJsonStr = retJsonStr.replace(/(<pre>)|(<pre style=.*">)/i, ""); retJsonStr = retJsonStr.replace("</pre>", ""); try { retJson = JSON.parse(retJsonStr).response; alu.nms.CpbErrors.handleServerResponse(retJson, retJson.data, null, onSuccess_c, null, alu.nms.NMSStrings.getString("cpb_app_uploadingAFileError") ); } catch (ex) { isc.warn(alu.nms.NMSStrings.getString("cpb_app_fileUpload_invalidServerResponse")); } // always clean up. iFrame.contentDocument.body.innerHTML = ""; } else { // we did not get any response from the server isc.warn(alu.nms.NMSStrings.getString("cpb_app_fileUpload_invalidServerResponse")); } }; fileUploadForm = isc.DynamicForm.create({ ID: "fileUploadForm", width: 450, encoding: "multipart", action: isc.Page.getAppDir() + "rest/cpb/v1/uploadFileHtml", target: "alu_nms_cpb_upload_iframe", fields: [ { ID: "fileUploadItem", id: "fileUploadItem", name: "file", type: "UploadItem", accept: "text/xml", showTitle: false, //title: alu.nms.NMSStrings.getString("cpb_app_uploadFileLabel"), prompt: alu.nms.NMSStrings.getString("cpb_app_maxFileSizePrompt"), width: 375, required: true, multiple: false, changed: function (form, item, value) { if (form) { let loadButton = form.getField('fileUploadLoadButton'); if (loadButton) { if (value !== null) { loadButton.focusInItem(); loadButton.setDisabled(false); } else { loadButton.setDisabled(true); } } } } }, { name: "fileUploadLoadButton", title: alu.nms.NMSStrings.getString("cpb_app_uploadFileButton"), type: "button", click: function () { if (fileUploadForm.getItem("file").getValue()) { fileUploadForm.submitForm(); uploadWindow.close(); } }, state: 'disabled' } ] }); fileUploadForm.getField('fileUploadLoadButton').setDisabled(true); uploadWindow = isc.Window.create({ ID: "uploadFileWindow", title: alu.nms.NMSStrings.getString("cpb_app_fileUploadTitle"), autoSize: true, autoCenter: true, isModal: true, showModalMask: true, autoDraw: false, showHeaderIcon: false, items: [fileUploadForm] }); uploadWindow.show(); }

    #2
    That's unreadable and lacks context that would be needed to know how to refactor it. However, if you want help upgrading legacy code, you should consider our Consulting services:

    https://smartclient.com/services/#consulting

    Comment


      #3
      Here is the refactored code

      addFileNoSmartClientServer: function () {
      var self = this, iFrame, fileUploadForm, uploadWindow, onSuccess_c;

      iFrame = document.getElementById("alu_nms_cpb_upload_iframe");

      onSuccess_c = function () {
      alu.nms.EBMessage.create({
      topic_s: alu.nms.CpbApp.CPB_CHANNEL,
      type_i: alu.nms.CpbApp.REFRESH_SYSTEMS_LIST_EVENT,
      originator_o: self,
      payload_o: null
      }).send();
      };

      iFrame.onload = function iFrameOnLoad() {

      var retJsonStr, retJson;

      retJsonStr = iFrame.contentDocument.body.innerHTML;

      if (retJsonStr) {
      // Some browsers add <pre> tags around the returned data
      // Chrome likes to place the 'style' attribute in the <pre> also...
      retJsonStr = retJsonStr.replace(/(<pre>)|(<pre style=.*">)/i, "");
      retJsonStr = retJsonStr.replace("</pre>", "");

      try {
      retJson = JSON.parse(retJsonStr).response;
      alu.nms.CpbErrors.handleServerResponse(retJson, retJson.data, null, onSuccess_c, null,
      alu.nms.NMSStrings.getString("cpb_app_uploadingAFileError")
      );
      } catch (ex) {
      isc.warn(alu.nms.NMSStrings.getString("cpb_app_fileUpload_invalidServerResponse"));
      }

      // always clean up.
      iFrame.contentDocument.body.innerHTML = "";

      } else {
      // we did not get any response from the server
      isc.warn(alu.nms.NMSStrings.getString("cpb_app_fileUpload_invalidServerResponse"));
      }
      };

      fileUploadForm = isc.DynamicForm.create({
      ID: "fileUploadForm",
      width: 450,
      encoding: "multipart",
      action: isc.Page.getAppDir() + "rest/cpb/v1/uploadFileHtml",
      target: "alu_nms_cpb_upload_iframe",
      fields:
      [
      {
      ID: "fileUploadItem",
      id: "fileUploadItem",
      name: "file",
      type: "UploadItem",
      accept: "text/xml",
      showTitle: false,
      //title: alu.nms.NMSStrings.getString("cpb_app_uploadFileLabel"),
      prompt: alu.nms.NMSStrings.getString("cpb_app_maxFileSizePrompt"),
      width: 375,
      required: true,
      multiple: false,
      changed: function (form, item, value) {
      if (form) {
      let loadButton = form.getField('fileUploadLoadButton');
      if (loadButton) {
      if (value !== null) {
      loadButton.focusInItem();
      loadButton.setDisabled(false);
      }
      else {
      loadButton.setDisabled(true);
      }
      }
      }
      }
      },
      {
      name: "fileUploadLoadButton",
      title: alu.nms.NMSStrings.getString("cpb_app_uploadFileButton"),
      type: "button",
      click: function () {
      if (fileUploadForm.getItem("file").getValue()) {
      fileUploadForm.submitForm();
      uploadWindow.close();
      }
      },
      state: 'disabled'
      }
      ]
      });

      fileUploadForm.getField('fileUploadLoadButton').setDisabled(true);

      uploadWindow = isc.Window.create({
      ID: "uploadFileWindow",
      title: alu.nms.NMSStrings.getString("cpb_app_fileUploadTitle"),
      autoSize: true,
      autoCenter: true,
      isModal: true,
      showModalMask: true,
      autoDraw: false,
      showHeaderIcon: false,
      items: [fileUploadForm]
      });

      uploadWindow.show();
      }

      Comment

      Working...
      X