convert cah.ajax into an object to make things a little neater
This commit is contained in:
parent
0183ca5357
commit
37cc770ed3
|
@ -8,25 +8,36 @@ cah.ajax = {};
|
||||||
cah.ajax.ErrorHandlers = {};
|
cah.ajax.ErrorHandlers = {};
|
||||||
cah.ajax.SuccessHandlers = {};
|
cah.ajax.SuccessHandlers = {};
|
||||||
|
|
||||||
// TODO run a timer to see if we have more than X pending requests and delay further ones until
|
/**
|
||||||
// we get results
|
* Create a new cah ajax helper.
|
||||||
cah.ajax.pendingRequests = {};
|
*
|
||||||
|
* @returns {cah.ajax.lib}
|
||||||
cah.ajax.serial = 0;
|
* @constructor
|
||||||
|
*/
|
||||||
|
cah.ajax.lib = function() {
|
||||||
|
// TODO run a timer to see if we have more than X pending requests and delay further ones until
|
||||||
|
// we get results
|
||||||
|
this.pendingRequests = {};
|
||||||
|
this.serial = 0;
|
||||||
|
};
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
/**
|
||||||
|
* Singleton instance for ajax utility.
|
||||||
|
*
|
||||||
|
* @type {cah.ajax.lib}
|
||||||
|
*/
|
||||||
|
cah.Ajax = new cah.ajax.lib();
|
||||||
$.ajaxSetup({
|
$.ajaxSetup({
|
||||||
cache : false,
|
cache : false,
|
||||||
error : cah.ajax.error,
|
context : cah.Ajax,
|
||||||
success : cah.ajax.done,
|
error : cah.Ajax.error,
|
||||||
|
success : cah.Ajax.done,
|
||||||
timeout : cah.DEBUG ? undefined : 10 * 1000, // 10 second timeout for normal requests
|
timeout : cah.DEBUG ? undefined : 10 * 1000, // 10 second timeout for normal requests
|
||||||
// timeout : 1, // 10 second timeout for normal requests
|
// timeout : 1, // 10 second timeout for normal requests
|
||||||
type : 'POST',
|
type : 'POST',
|
||||||
url : '/cah/AjaxServlet'
|
url : '/cah/AjaxServlet'
|
||||||
});
|
});
|
||||||
|
|
||||||
// see if we already exist on the server so we can resume
|
|
||||||
cah.ajax.request("firstload", {});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,48 +53,48 @@ $(document).ready(function() {
|
||||||
* @param {?function(jqXHR,textStatus,errorThrown)}
|
* @param {?function(jqXHR,textStatus,errorThrown)}
|
||||||
* [opt_errback] Optional error callback.
|
* [opt_errback] Optional error callback.
|
||||||
*/
|
*/
|
||||||
cah.ajax.request = function(op, data, opt_errback) {
|
cah.ajax.lib.prototype.request = function(op, data, opt_errback) {
|
||||||
data.op = op;
|
data.op = op;
|
||||||
data.serial = cah.ajax.serial++;
|
data.serial = this.serial++;
|
||||||
var jqXHR = $.ajax({
|
var jqXHR = $.ajax({
|
||||||
data : data
|
data : data
|
||||||
});
|
});
|
||||||
cah.ajax.pendingRequests[data.serial] = data;
|
this.pendingRequests[data.serial] = data;
|
||||||
cah.log.debug("ajax req", data);
|
cah.log.debug("ajax req", data);
|
||||||
if (opt_errback) {
|
if (opt_errback) {
|
||||||
jqXHR.fail(opt_errback);
|
jqXHR.fail(opt_errback);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
cah.ajax.error = function(jqXHR, textStatus, errorThrown) {
|
cah.ajax.lib.prototype.error = function(jqXHR, textStatus, errorThrown) {
|
||||||
// TODO deal with this somehow
|
// TODO deal with this somehow
|
||||||
// and figure out which request it was so we can remove it from pending
|
// and figure out which request it was so we can remove it from pending
|
||||||
debugger;
|
debugger;
|
||||||
cah.log.error(textStatus);
|
cah.log.error(textStatus);
|
||||||
};
|
};
|
||||||
|
|
||||||
cah.ajax.done = function(data) {
|
cah.ajax.lib.prototype.done = function(data) {
|
||||||
cah.log.debug("ajax done", data);
|
cah.log.debug("ajax done", data);
|
||||||
if (data['error']) {
|
if (data['error']) {
|
||||||
// TODO cancel any timers or whatever we may have, and disable interface
|
// TODO cancel any timers or whatever we may have, and disable interface
|
||||||
var req = cah.ajax.pendingRequests[data.serial];
|
var req = this.pendingRequests[data.serial];
|
||||||
if (req && cah.ajax.ErrorHandlers[req.op]) {
|
if (req && cah.ajax.ErrorHandlers[req.op]) {
|
||||||
cah.ajax.ErrorHandlers[req.op](data);
|
cah.ajax.ErrorHandlers[req.op](data);
|
||||||
} else {
|
} else {
|
||||||
cah.log.error(data.error_message);
|
cah.log.error(data.error_message);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var req = cah.ajax.pendingRequests[data.serial];
|
var req = this.pendingRequests[data.serial];
|
||||||
if (req && cah.ajax.SuccessHandlers[req.op]) {
|
if (req && cah.ajax.SuccessHandlers[req.op]) {
|
||||||
cah.ajax.SuccessHandlers[req.op](data);
|
cah.ajax.SuccessHandlers[req.op](data);
|
||||||
} else if (req) {
|
} else if (req) {
|
||||||
cah.log.error("Unhandled response for op " + req.op);
|
cah.log.error("Unhandled response for op " + req.op);
|
||||||
} else {
|
} else {
|
||||||
cah.log.error("Unknown response for serial " + data.serial);
|
cah.log.error("Response for unknown serial " + data.serial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.serial >= 0 && cah.ajax.pendingRequests[data.serial]) {
|
if (data.serial >= 0 && this.pendingRequests[data.serial]) {
|
||||||
delete cah.ajax.pendingRequests[data.serial];
|
delete this.pendingRequests[data.serial];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue