Skip to content

Commit 0e9d184

Browse files
committed
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.
1 parent 7970cd3 commit 0e9d184

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

src/js/api.library.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ api.content.getParam = function (pKey) {
126126
* @param {*} pSelectorContainer
127127
* @param {*} pRelativeURL
128128
* @param {*} pParams
129+
* @param {*} pAppend
129130
*/
130-
api.content.load = function (pSelectorContainer, pRelativeURL, pParams) {
131+
api.content.load = function (pSelectorContainer, pRelativeURL, pParams, pAppend) {
131132
// Default parameters
132133
pParams = pParams || {};
134+
pAppend = pAppend || false;
133135

134136
// Validate the Relative URL
135137
var uri = new URI(pRelativeURL);
@@ -146,9 +148,14 @@ api.content.load = function (pSelectorContainer, pRelativeURL, pParams) {
146148
async: false,
147149
success: function (response) {
148150
api.content.params = pParams;
149-
$(pSelectorContainer).empty().html(response).promise().done(function () {
150-
api.content.params = {};
151-
});
151+
if (pAppend)
152+
$(pSelectorContainer).append(response).promise().done(function () {
153+
api.content.params = {};
154+
});
155+
else
156+
$(pSelectorContainer).empty().html(response).promise().done(function () {
157+
api.content.params = {};
158+
});
152159
}
153160
});
154161
};
@@ -311,22 +318,34 @@ api.ajax.callback = function (pFunction, pResponse, pParams) {
311318

312319
/**
313320
* Load a configuration file
314-
* Use Javascript rather than JQuery because the configuration must load synchronously before $(document).ready
315-
* @param {*} url
316-
* @param {*} callback
321+
* @param {*} pUrl
322+
* @param {*} pCallback
323+
* @param {*} pAjaxParams
317324
*/
318-
api.ajax.config = function (url, callback) {
319-
var xobj = new XMLHttpRequest();
320-
xobj.overrideMimeType("application/json");
321-
xobj.open('GET', url, false);
322-
xobj.onreadystatechange = function () {
323-
if (xobj.readyState == 4 && xobj.status == "200") {
324-
callback(xobj.responseText);
325-
} else {
326-
console.log("Internal Error: the configuration file \"" + url + "\" is missing or invalid.");
325+
api.ajax.config = function (pUrl, pCallback, pAjaxParams) {
326+
// Default AJAX parameters
327+
pAjaxParams = pAjaxParams || {};
328+
pAjaxParams.method = pAjaxParams.method || 'GET';
329+
pAjaxParams.dataType = pAjaxParams.dataType || 'json';
330+
pAjaxParams.jsonp = pAjaxParams.jsonp || false; // Fix for "??" JQuery placeholder
331+
pAjaxParams.timeout = pAjaxParams.timeout || 60000;
332+
pAjaxParams.async = pAjaxParams.async || false;
333+
334+
ajaxParams = {
335+
url: pUrl,
336+
success: function (response) {
337+
pCallback(response);
338+
},
339+
error: function (jqXHR, textStatus, errorThrown) {
340+
// Log the issue rather than popping it in a Bootstrap modal because the document may not be ready yet
341+
console.log("An Internal Server has occurred: the configuration file \"" + url + "\" is missing or invalid.");
327342
}
328343
};
329-
xobj.send(null);
344+
345+
// Merge ajax parameters
346+
$.extend(ajaxParams, pAjaxParams);
347+
// Run the Ajax call
348+
$.ajax(ajaxParams);
330349
};
331350

332351
/*******************************************************************************

test/js/app.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ app.config = {};
1111

1212
// Load the config.json into the application
1313
api.ajax.config("config/config.json", function (config) {
14-
// Parse JSON string into object
15-
app.config = JSON.parse(config);
14+
app.config = config;
1615
});

0 commit comments

Comments
 (0)