ajax.js | |
---|---|
/*global Ply, jQuery */
/*jshint eqeqeq: true, curly: true, white: true */ | |
The Ajax module is a simple wrapper around jQuery's | |
Define the | Ply.ajax = (function ($) {
return {
request: function (request, success, failure) { |
Create alias for request to avoid reference errors. | var r = request || {}, |
Default to | type = r.type || "GET",
url = r.url || '',
data = r.data || {}; |
Send along a | if (Ply.core.debugOn) {
data.debug = true;
} |
Surround ajax request in a try/catch block to catch any possible errors. | try { |
We return the result of calling | return $.ajax({
type: type,
url: url,
data: data,
success: function (response, status, xhr) { |
Call the | if (success && typeof success === 'function') {
success(response, status, xhr);
}
},
error: function (xhr, status, error) { |
On error, log an AjaxError to the the | Ply.core.log('AjaxError: ' + status + ' - ' + url, 'warn'); |
Call the | Ply.core.error({
name: 'AjaxError', |
Send the status code in the message. | message: 'Status: ' + status,
description: 'URL: ' + url
}, 1); |
Call the | if (failure && typeof failure === 'function') {
failure(xhr, status, error);
}
}
});
} |
Log and post any error that may have occurred. | catch (ex) {
Ply.core.log(ex.message);
Ply.core.error(ex, 1);
}
}
}; |
Alias | })(jQuery); |
↪ Read | |