make ajax requests come from a builder to work on removing constant strings
This commit is contained in:
parent
7f1158f59c
commit
9e8c46ec2f
|
@ -14,6 +14,7 @@
|
|||
<script type="text/javascript" src="js/cah.longpoll.js"></script>
|
||||
<script type="text/javascript" src="js/cah.longpoll.handlers.js"></script>
|
||||
<script type="text/javascript" src="js/cah.ajax.js"></script>
|
||||
<script type="text/javascript" src="js/cah.ajax.builder.js"></script>
|
||||
<script type="text/javascript" src="js/cah.ajax.handlers.js"></script>
|
||||
<script type="text/javascript" src="js/cah.app.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="cah.css" media="screen" />
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* 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 = {};
|
||||
|
||||
this.data.op = op;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
this.errback = errback;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Run the ajax request.
|
||||
*/
|
||||
cah.ajax.Builder.prototype.run = function() {
|
||||
this.data.serial = cah.ajax.Builder.serial++;
|
||||
cah.Ajax.requestWithBuilder(this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string}
|
||||
* nickname Nickname field to use in the request.
|
||||
* @returns {cah.ajax.Builder} This object.
|
||||
*/
|
||||
cah.ajax.Builder.prototype.withNickname = function(nickname) {
|
||||
this.data.nickname = nickname;
|
||||
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) {
|
||||
this.data.message = message;
|
||||
return this;
|
||||
};
|
|
@ -1,5 +1,7 @@
|
|||
/**
|
||||
* AJAX callback handlers. TODO make this individual files instead of all in one.
|
||||
* AJAX callback handlers.
|
||||
*
|
||||
* TODO make this individual files instead of all in one.
|
||||
*
|
||||
* @author ajanata
|
||||
*/
|
||||
|
@ -41,7 +43,7 @@ cah.ajax.ErrorHandlers.firstload = function(data) {
|
|||
cah.ajax.after_registered = function() {
|
||||
cah.log.debug("done registering");
|
||||
// TODO once there are channels, this needs to specify the global channel
|
||||
cah.Ajax.request("names", {});
|
||||
cah.Ajax.build("names").run();
|
||||
cah.longpoll.longPoll();
|
||||
};
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ 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() {
|
||||
|
@ -34,7 +33,6 @@ $(document).ready(function() {
|
|||
error : cah.Ajax.error,
|
||||
success : cah.Ajax.done,
|
||||
timeout : cah.DEBUG ? undefined : 10 * 1000, // 10 second timeout for normal requests
|
||||
// timeout : 1, // 10 second timeout for normal requests
|
||||
type : 'POST',
|
||||
url : '/cah/AjaxServlet'
|
||||
});
|
||||
|
@ -46,23 +44,17 @@ $(document).ready(function() {
|
|||
*
|
||||
* This should be used for data sent to the server, not long-polling.
|
||||
*
|
||||
* @param {string}
|
||||
* op Operation code for the request.
|
||||
* @param {object}
|
||||
* data Parameter map to send for the request.
|
||||
* @param {?function(jqXHR,textStatus,errorThrown)}
|
||||
* [opt_errback] Optional error callback.
|
||||
* @param {cah.ajax.Builder}
|
||||
* builder Request builder containing data to use.
|
||||
*/
|
||||
cah.ajax.lib.prototype.request = function(op, data, opt_errback) {
|
||||
data.op = op;
|
||||
data.serial = this.serial++;
|
||||
cah.ajax.lib.prototype.requestWithBuilder = function(builder) {
|
||||
var jqXHR = $.ajax({
|
||||
data : data
|
||||
data : builder.data
|
||||
});
|
||||
this.pendingRequests[data.serial] = data;
|
||||
cah.log.debug("ajax req", data);
|
||||
if (opt_errback) {
|
||||
jqXHR.fail(opt_errback);
|
||||
this.pendingRequests[builder.data.serial] = builder.data;
|
||||
cah.log.debug("ajax req", builder.data);
|
||||
if (builder.errback) {
|
||||
jqXHR.fail(builder.errback);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -98,3 +90,14 @@ cah.ajax.lib.prototype.done = function(data) {
|
|||
delete this.pendingRequests[data.serial];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a builder for an ajax request.
|
||||
*
|
||||
* @param {string}
|
||||
* op Operation code for the request.
|
||||
* @returns {cah.ajax.Builder} Builder to create the request.
|
||||
*/
|
||||
cah.ajax.lib.prototype.build = function(op) {
|
||||
return new cah.ajax.Builder(op);
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
$(document).ready(function() {
|
||||
// see if we already exist on the server so we can resume
|
||||
cah.Ajax.request("firstload", {});
|
||||
cah.Ajax.build("firstload").run();
|
||||
|
||||
// TODO see if we have a stored nickname somewhere
|
||||
$("#nicknameconfirm").click(nicknameconfirm_click);
|
||||
|
@ -31,9 +31,7 @@ function nickbox_keyup(e) {
|
|||
|
||||
function nicknameconfirm_click(e) {
|
||||
var nickname = $.trim($("#nickname").val());
|
||||
cah.Ajax.request("register", {
|
||||
nickname : nickname,
|
||||
});
|
||||
cah.Ajax.build("register").withNickname(nickname).run();
|
||||
}
|
||||
|
||||
function chat_keyup(e) {
|
||||
|
@ -46,14 +44,12 @@ function chat_keyup(e) {
|
|||
function chatsubmit_click(e) {
|
||||
var text = $.trim($("#chat").val());
|
||||
// TODO when I get multiple channels working, this needs to know active and pass it
|
||||
cah.Ajax.request("chat", {
|
||||
message : text,
|
||||
});
|
||||
cah.Ajax.build("chat").withMessage(text).run();
|
||||
cah.log.status("<" + cah.nickname + "> " + text);
|
||||
$("#chat").val("");
|
||||
$("#chat").focus();
|
||||
}
|
||||
|
||||
function logout_click(e) {
|
||||
cah.Ajax.request("logout", {});
|
||||
cah.Ajax.build("logout").run();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue