2012-02-06 22:00:24 +00:00
|
|
|
/*
|
2012-02-02 22:47:23 +00:00
|
|
|
* Copyright (c) 2012, Andy Janata
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
|
|
* provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
|
|
* and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
|
|
* conditions and the following disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
|
|
|
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/**
|
2012-02-06 22:00:24 +00:00
|
|
|
* An AJAX helper. This wraps around jQuery's AJAX function, and dispatches results to the
|
|
|
|
* appropriate handler.
|
2011-12-25 03:38:02 +00:00
|
|
|
*
|
2012-02-06 22:00:24 +00:00
|
|
|
* @author Andy Janata (ajanata@socialgamer.net)
|
2011-12-25 03:38:02 +00:00
|
|
|
* @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
|
2012-02-06 22:00:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Id->data map of active requests. This is so we can map back to the request data when we get a
|
|
|
|
* response.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.pendingRequests_ = {};
|
2011-12-25 03:38:02 +00:00
|
|
|
};
|
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',
|
2012-01-31 07:49:46 +00:00
|
|
|
url : cah.AJAX_URI,
|
2011-12-23 02:48:20 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an ajax request to the server, and store that the request was sent so we know when it gets
|
2012-02-06 22:00:24 +00:00
|
|
|
* responded to. This should be used for data sent to the server, not long-polling.
|
2011-12-23 02:48:20 +00:00
|
|
|
*
|
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-02-06 22:00:24 +00:00
|
|
|
this.pendingRequests_[builder.getSerial()] = builder;
|
2012-01-12 22:25:04 +00:00
|
|
|
cah.log.debug("ajax req", builder.data);
|
|
|
|
if (builder.errback) {
|
|
|
|
jqXHR.fail(builder.errback);
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handler for when there is a communication-level error with an ajax request. This will likely be
|
|
|
|
* because the server isn't responding or returned malformed data.
|
|
|
|
*
|
|
|
|
* @param {Object}
|
|
|
|
* jqXHR The jQueryXmlHttpRequest.
|
|
|
|
* @param {String}
|
|
|
|
* textStatus Status message.
|
|
|
|
* @param {String}
|
|
|
|
* errorThrown Error cause.
|
|
|
|
*/
|
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;
|
2012-01-31 07:49:46 +00:00
|
|
|
cah.log.error(textStatus + " " + errorThrown);
|
2011-12-23 02:48:20 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handler for when an ajax request is completed sucessfully. Examine the result and dispatch it to
|
|
|
|
* the appropriate handler.
|
|
|
|
*
|
|
|
|
* @param {Object}
|
|
|
|
* data Data returned from the server.
|
|
|
|
*/
|
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);
|
2012-01-17 00:28:21 +00:00
|
|
|
if (data[cah.$.AjaxResponse.ERROR]) {
|
2011-12-23 02:48:20 +00:00
|
|
|
// TODO cancel any timers or whatever we may have, and disable interface
|
2012-01-31 07:49:46 +00:00
|
|
|
// or probably in individual error handlers as there are some errors that are fine like
|
|
|
|
// "you don't have that card" etc.
|
2012-02-06 22:00:24 +00:00
|
|
|
var req = this.pendingRequests_[data[cah.$.AjaxResponse.SERIAL]];
|
2012-01-17 00:28:21 +00:00
|
|
|
if (req && cah.ajax.ErrorHandlers[req.getOp()]) {
|
|
|
|
cah.ajax.ErrorHandlers[req.getOp()](data);
|
2011-12-23 02:48:20 +00:00
|
|
|
} else {
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.log.error(cah.$.ErrorCode_msg[data[cah.$.AjaxResponse.ERROR_CODE]]);
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-02-06 22:00:24 +00:00
|
|
|
var req = this.pendingRequests_[data[cah.$.AjaxResponse.SERIAL]];
|
2012-01-17 00:28:21 +00:00
|
|
|
if (req && cah.ajax.SuccessHandlers[req.getOp()]) {
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.ajax.SuccessHandlers[req.getOp()](data, req.data);
|
2011-12-23 02:48:20 +00:00
|
|
|
} else if (req) {
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.log.error("Unhandled response for op " + req.getOp());
|
2011-12-23 02:48:20 +00:00
|
|
|
} else {
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.log.error("Response for unknown serial " + data[cah.$.AjaxResponse.SERIAL]);
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 00:28:21 +00:00
|
|
|
var serial = data[cah.$.AjaxResponse.SERIAL];
|
2012-02-06 22:00:24 +00:00
|
|
|
if (serial >= 0 && this.pendingRequests_[serial]) {
|
|
|
|
delete this.pendingRequests_[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);
|
|
|
|
};
|