From 9e8c46ec2f585d0c8ca7a1ec7cd8a9e5f344c26a Mon Sep 17 00:00:00 2001 From: Andy Janata Date: Thu, 12 Jan 2012 14:25:04 -0800 Subject: [PATCH] make ajax requests come from a builder to work on removing constant strings --- WebContent/index.jsp | 1 + WebContent/js/cah.ajax.builder.js | 79 ++++++++++++++++++++++++++++++ WebContent/js/cah.ajax.handlers.js | 6 ++- WebContent/js/cah.ajax.js | 35 +++++++------ WebContent/js/cah.app.js | 12 ++--- 5 files changed, 107 insertions(+), 26 deletions(-) create mode 100644 WebContent/js/cah.ajax.builder.js diff --git a/WebContent/index.jsp b/WebContent/index.jsp index af59835..c5bd08f 100644 --- a/WebContent/index.jsp +++ b/WebContent/index.jsp @@ -14,6 +14,7 @@ + diff --git a/WebContent/js/cah.ajax.builder.js b/WebContent/js/cah.ajax.builder.js new file mode 100644 index 0000000..2dabc18 --- /dev/null +++ b/WebContent/js/cah.ajax.builder.js @@ -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; +}; diff --git a/WebContent/js/cah.ajax.handlers.js b/WebContent/js/cah.ajax.handlers.js index 96b1e3b..8f78a2c 100644 --- a/WebContent/js/cah.ajax.handlers.js +++ b/WebContent/js/cah.ajax.handlers.js @@ -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(); }; diff --git a/WebContent/js/cah.ajax.js b/WebContent/js/cah.ajax.js index 902bd33..35460cd 100644 --- a/WebContent/js/cah.ajax.js +++ b/WebContent/js/cah.ajax.js @@ -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); +}; diff --git a/WebContent/js/cah.app.js b/WebContent/js/cah.app.js index 74af5b5..6b25247 100644 --- a/WebContent/js/cah.app.js +++ b/WebContent/js/cah.app.js @@ -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(); }