Skip to content

Commit

Permalink
Method api.ajax.config refactored and extended:
Browse files Browse the repository at this point in the history
>> 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.
  • Loading branch information
lorenzobruni committed Apr 15, 2020
1 parent 7970cd3 commit 0e9d184
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
53 changes: 36 additions & 17 deletions src/js/api.library.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = {};
});
}
});
};
Expand Down Expand Up @@ -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);
};

/*******************************************************************************
Expand Down
3 changes: 1 addition & 2 deletions test/js/app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

0 comments on commit 0e9d184

Please sign in to comment.