2011-12-23 02:48:20 +00:00
|
|
|
/**
|
|
|
|
* AJAX utility functions for cah. Core library. Individual handlers should be elsewhere.
|
|
|
|
*
|
|
|
|
* @author ajanata
|
|
|
|
*/
|
|
|
|
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax = {};
|
|
|
|
cah.Ajax.instance = {};
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.ajax = {};
|
|
|
|
cah.ajax.ErrorHandlers = {};
|
|
|
|
cah.ajax.SuccessHandlers = {};
|
|
|
|
|
2011-12-25 03:38:02 +00:00
|
|
|
/**
|
|
|
|
* Create a new cah ajax helper.
|
|
|
|
*
|
|
|
|
* @returns {cah.ajax.lib}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax = function() {
|
2011-12-25 03:38:02 +00:00
|
|
|
// TODO run a timer to see if we have more than X pending requests and delay further ones until
|
|
|
|
// we get results
|
|
|
|
this.pendingRequests = {};
|
|
|
|
};
|
2011-12-23 02:48:20 +00:00
|
|
|
|
|
|
|
$(document).ready(function() {
|
2011-12-25 03:38:02 +00:00
|
|
|
/**
|
|
|
|
* Singleton instance for ajax utility.
|
|
|
|
*
|
2012-01-13 04:05:39 +00:00
|
|
|
* @type {cah.Ajax}
|
2011-12-25 03:38:02 +00:00
|
|
|
*/
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax.instance = new cah.Ajax();
|
2011-12-23 02:48:20 +00:00
|
|
|
$.ajaxSetup({
|
|
|
|
cache : false,
|
2012-01-13 01:05:09 +00:00
|
|
|
context : cah.Ajax.instance,
|
|
|
|
error : cah.Ajax.instance.error,
|
|
|
|
success : cah.Ajax.instance.done,
|
2011-12-23 02:48:20 +00:00
|
|
|
timeout : cah.DEBUG ? undefined : 10 * 1000, // 10 second timeout for normal requests
|
|
|
|
type : 'POST',
|
|
|
|
url : '/cah/AjaxServlet'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an ajax request to the server, and store that the request was sent so we know when it gets
|
|
|
|
* responded to.
|
|
|
|
*
|
|
|
|
* This should be used for data sent to the server, not long-polling.
|
|
|
|
*
|
2012-01-12 22:25:04 +00:00
|
|
|
* @param {cah.ajax.Builder}
|
|
|
|
* builder Request builder containing data to use.
|
2011-12-23 02:48:20 +00:00
|
|
|
*/
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax.prototype.requestWithBuilder = function(builder) {
|
2011-12-23 02:48:20 +00:00
|
|
|
var jqXHR = $.ajax({
|
2012-01-12 22:25:04 +00:00
|
|
|
data : builder.data
|
2011-12-23 02:48:20 +00:00
|
|
|
});
|
2012-01-12 22:25:04 +00:00
|
|
|
this.pendingRequests[builder.data.serial] = builder.data;
|
|
|
|
cah.log.debug("ajax req", builder.data);
|
|
|
|
if (builder.errback) {
|
|
|
|
jqXHR.fail(builder.errback);
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax.prototype.error = function(jqXHR, textStatus, errorThrown) {
|
2011-12-23 02:48:20 +00:00
|
|
|
// TODO deal with this somehow
|
|
|
|
// and figure out which request it was so we can remove it from pending
|
|
|
|
debugger;
|
|
|
|
cah.log.error(textStatus);
|
|
|
|
};
|
|
|
|
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax.prototype.done = function(data) {
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.log.debug("ajax done", data);
|
|
|
|
if (data['error']) {
|
|
|
|
// TODO cancel any timers or whatever we may have, and disable interface
|
2011-12-25 03:38:02 +00:00
|
|
|
var req = this.pendingRequests[data.serial];
|
2011-12-23 02:48:20 +00:00
|
|
|
if (req && cah.ajax.ErrorHandlers[req.op]) {
|
|
|
|
cah.ajax.ErrorHandlers[req.op](data);
|
|
|
|
} else {
|
2012-01-16 22:33:58 +00:00
|
|
|
cah.log.error(cah.$.ErrorCode_msg[data.error_code]);
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-12-25 03:38:02 +00:00
|
|
|
var req = this.pendingRequests[data.serial];
|
2011-12-23 02:48:20 +00:00
|
|
|
if (req && cah.ajax.SuccessHandlers[req.op]) {
|
|
|
|
cah.ajax.SuccessHandlers[req.op](data);
|
|
|
|
} else if (req) {
|
|
|
|
cah.log.error("Unhandled response for op " + req.op);
|
|
|
|
} else {
|
2011-12-25 03:38:02 +00:00
|
|
|
cah.log.error("Response for unknown serial " + data.serial);
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-25 03:38:02 +00:00
|
|
|
if (data.serial >= 0 && this.pendingRequests[data.serial]) {
|
|
|
|
delete this.pendingRequests[data.serial];
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
};
|
2012-01-12 22:25:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a builder for an ajax request.
|
|
|
|
*
|
|
|
|
* @param {string}
|
|
|
|
* op Operation code for the request.
|
|
|
|
* @returns {cah.ajax.Builder} Builder to create the request.
|
|
|
|
*/
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax.build = function(op) {
|
2012-01-12 22:25:04 +00:00
|
|
|
return new cah.ajax.Builder(op);
|
|
|
|
};
|