2012-01-12 22:25:04 +00:00
|
|
|
/**
|
|
|
|
* Builder for ajax data.
|
|
|
|
*
|
|
|
|
* @author ajanata
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builder for ajax requests. This contains methods to add every possible parameter to an ajax
|
|
|
|
* request, even if it doesn't make sense for the operation code.
|
|
|
|
*
|
|
|
|
* @param {string}
|
|
|
|
* op The operation code for the ajax request.
|
|
|
|
* @returns {cah.ajax.Builder}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder = function(op) {
|
|
|
|
/**
|
|
|
|
* The data for this request.
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this.data = {};
|
|
|
|
|
2012-01-17 00:28:21 +00:00
|
|
|
this.data[cah.$.AjaxRequest.OP] = op;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this request has been run or not.
|
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.run_ = false;
|
2012-01-12 22:25:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error callback for this request.
|
|
|
|
*
|
|
|
|
* @type {?function(jqXHR,textStatus,errorThrown)}
|
|
|
|
*/
|
|
|
|
this.errback = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serial counter for ajax requests.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.serial = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an error callback for the request.
|
|
|
|
*
|
|
|
|
* @param {?function(jqXHR,textStatus,errorThrown)}
|
|
|
|
* [opt_errback] Optional error callback.
|
|
|
|
* @returns {cah.ajax.Builder}
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withErrback = function(errback) {
|
2012-01-17 00:28:21 +00:00
|
|
|
this.assertNotExecuted();
|
2012-01-12 22:25:04 +00:00
|
|
|
this.errback = errback;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the ajax request.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.run = function() {
|
2012-01-17 00:28:21 +00:00
|
|
|
this.assertNotExecuted();
|
|
|
|
this.run_ = true;
|
|
|
|
|
|
|
|
this.data[cah.$.AjaxRequest.SERIAL] = cah.ajax.Builder.serial++;
|
2012-01-13 01:05:09 +00:00
|
|
|
cah.Ajax.instance.requestWithBuilder(this);
|
2012-01-12 22:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string}
|
|
|
|
* nickname Nickname field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withNickname = function(nickname) {
|
2012-01-17 00:28:21 +00:00
|
|
|
this.assertNotExecuted();
|
|
|
|
this.data[cah.$.AjaxRequest.NICKNAME] = nickname;
|
2012-01-12 22:25:04 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string}
|
|
|
|
* message Message field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withMessage = function(message) {
|
2012-01-17 00:28:21 +00:00
|
|
|
this.assertNotExecuted();
|
|
|
|
this.data[cah.$.AjaxRequest.MESSAGE] = message;
|
2012-01-12 22:25:04 +00:00
|
|
|
return this;
|
|
|
|
};
|
2012-01-17 00:28:21 +00:00
|
|
|
|
2012-01-19 00:42:18 +00:00
|
|
|
/**
|
|
|
|
* @param {number}
|
|
|
|
* gameId Game id field to use in the request.
|
|
|
|
* @returns {cah.ajax.Builder} This object.
|
|
|
|
*/
|
|
|
|
cah.ajax.Builder.prototype.withGameId = function(gameId) {
|
|
|
|
this.assertNotExecuted();
|
|
|
|
this.data[cah.$.AjaxRequest.GAME_ID] = gameId;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2012-01-17 00:28:21 +00:00
|
|
|
cah.ajax.Builder.prototype.assertNotExecuted = function() {
|
|
|
|
if (this.run_) {
|
|
|
|
throw "Request already executed.";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.ajax.Builder.prototype.assertExecuted = function() {
|
|
|
|
if (!this.run_) {
|
|
|
|
throw "Request not yet executed.";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.ajax.Builder.prototype.getOp = function() {
|
|
|
|
this.assertExecuted();
|
|
|
|
return this.data[cah.$.AjaxRequest.OP];
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.ajax.Builder.prototype.getSerial = function() {
|
|
|
|
this.assertExecuted();
|
|
|
|
return this.data[cah.$.AjaxRequest.SERIAL];
|
|
|
|
};
|