From 0e9d18420716b404db5c2025698d9093680f9699 Mon Sep 17 00:00:00 2001 From: PC-Axis Date: Wed, 15 Apr 2020 08:47:59 +0100 Subject: [PATCH] Method api.ajax.config refactored and extended: >> Ajax call refactored using the JQuery $.ajax method >> Optional parameter pAjaxParams of type {} implemented to override the $.ajax parameters Method api.content.load extended: >> Optional parameter pAppend of type boolean implemented to append the loaded content to the Selector container rather than empty it first. --- src/js/api.library.js | 53 +++++++++++++++++++++++++++++-------------- test/js/app.config.js | 3 +-- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/js/api.library.js b/src/js/api.library.js index b8eb56b..8f24718 100644 --- a/src/js/api.library.js +++ b/src/js/api.library.js @@ -126,10 +126,12 @@ api.content.getParam = function (pKey) { * @param {*} pSelectorContainer * @param {*} pRelativeURL * @param {*} pParams + * @param {*} pAppend */ -api.content.load = function (pSelectorContainer, pRelativeURL, pParams) { +api.content.load = function (pSelectorContainer, pRelativeURL, pParams, pAppend) { // Default parameters pParams = pParams || {}; + pAppend = pAppend || false; // Validate the Relative URL var uri = new URI(pRelativeURL); @@ -146,9 +148,14 @@ api.content.load = function (pSelectorContainer, pRelativeURL, pParams) { async: false, success: function (response) { api.content.params = pParams; - $(pSelectorContainer).empty().html(response).promise().done(function () { - api.content.params = {}; - }); + if (pAppend) + $(pSelectorContainer).append(response).promise().done(function () { + api.content.params = {}; + }); + else + $(pSelectorContainer).empty().html(response).promise().done(function () { + api.content.params = {}; + }); } }); }; @@ -311,22 +318,34 @@ api.ajax.callback = function (pFunction, pResponse, pParams) { /** * Load a configuration file - * Use Javascript rather than JQuery because the configuration must load synchronously before $(document).ready - * @param {*} url - * @param {*} callback + * @param {*} pUrl + * @param {*} pCallback + * @param {*} pAjaxParams */ -api.ajax.config = function (url, callback) { - var xobj = new XMLHttpRequest(); - xobj.overrideMimeType("application/json"); - xobj.open('GET', url, false); - xobj.onreadystatechange = function () { - if (xobj.readyState == 4 && xobj.status == "200") { - callback(xobj.responseText); - } else { - console.log("Internal Error: the configuration file \"" + url + "\" is missing or invalid."); +api.ajax.config = function (pUrl, pCallback, pAjaxParams) { + // Default AJAX parameters + pAjaxParams = pAjaxParams || {}; + pAjaxParams.method = pAjaxParams.method || 'GET'; + pAjaxParams.dataType = pAjaxParams.dataType || 'json'; + pAjaxParams.jsonp = pAjaxParams.jsonp || false; // Fix for "??" JQuery placeholder + pAjaxParams.timeout = pAjaxParams.timeout || 60000; + pAjaxParams.async = pAjaxParams.async || false; + + ajaxParams = { + url: pUrl, + success: function (response) { + pCallback(response); + }, + error: function (jqXHR, textStatus, errorThrown) { + // Log the issue rather than popping it in a Bootstrap modal because the document may not be ready yet + console.log("An Internal Server has occurred: the configuration file \"" + url + "\" is missing or invalid."); } }; - xobj.send(null); + + // Merge ajax parameters + $.extend(ajaxParams, pAjaxParams); + // Run the Ajax call + $.ajax(ajaxParams); }; /******************************************************************************* diff --git a/test/js/app.config.js b/test/js/app.config.js index d16cdb7..b7ee36e 100644 --- a/test/js/app.config.js +++ b/test/js/app.config.js @@ -11,6 +11,5 @@ app.config = {}; // Load the config.json into the application api.ajax.config("config/config.json", function (config) { - // Parse JSON string into object - app.config = JSON.parse(config); + app.config = config; }); \ No newline at end of file