From d26d887933c907cc0c8283e1b1f17a5b0f8e2118 Mon Sep 17 00:00:00 2001 From: Mark Maglana Date: Mon, 26 Jul 2010 14:54:14 +0800 Subject: [PATCH] Prevent race conditions in ajax calls --- assets/javascripts/backlog.js | 4 ++-- assets/javascripts/common.js | 22 ++++++++++++++++++++++ assets/javascripts/index_main.js | 4 ++-- assets/javascripts/story.js | 3 ++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/assets/javascripts/backlog.js b/assets/javascripts/backlog.js index 4d823562f8..ed3fdfda9f 100755 --- a/assets/javascripts/backlog.js +++ b/assets/javascripts/backlog.js @@ -172,7 +172,7 @@ RB.Backlog = RB.Object.create(RB.Model, { }, loadStoryTemplate: function(){ - $.ajax({ + RB.ajax({ type: "GET", async: false, url: RB.urlFor['new_story'] + "?project_id=" + RB.constants.project_id, @@ -227,7 +227,7 @@ RB.Backlog = RB.Object.create(RB.Model, { } }); - $.ajax({ + RB.ajax({ type: "POST", url: RB.urlFor['update_backlog'], data: editors.serialize() + "&id=" + j.find('.id').text(), diff --git a/assets/javascripts/common.js b/assets/javascripts/common.js index 77c9c6117a..df8c7f4a1e 100755 --- a/assets/javascripts/common.js +++ b/assets/javascripts/common.js @@ -47,6 +47,28 @@ RB.dialog = RB.Object.create({ } }); +RB.ajaxQueue = new Array() +RB.ajaxOngoing = false; + +RB.ajax = function(options){ + RB.ajaxQueue.push(options); + if(!RB.ajaxOngoing){ RB.processAjaxQueue(); } +} + +RB.processAjaxQueue = function(){ + var options = RB.ajaxQueue.shift(); + + if(options!=null){ + RB.ajaxOngoing = true; + $.ajax(options); + } +} + +$(document).ajaxComplete(function(event, xhr, settings){ + RB.ajaxOngoing = false; + RB.processAjaxQueue(); +}); + // Modify the ajax request before being sent to the server $(document).ajaxSend(function(event, request, settings) { var c = RB.constants; diff --git a/assets/javascripts/index_main.js b/assets/javascripts/index_main.js index 7fed793fc1..96b14185ed 100755 --- a/assets/javascripts/index_main.js +++ b/assets/javascripts/index_main.js @@ -31,13 +31,13 @@ RB.indexMain = RB.Object.create({ }, loadData: function(){ - $('body').addClass('loading'); - $.ajax({ + RB.ajax({ type: "GET", url: RB.urlFor['list_stories'], data: { after : $('#last_updated').text(), project_id: RB.constants.project_id }, + beforeSend: function(){ $('body').addClass('loading'); }, success: RB.indexMain.refresh }); }, diff --git a/assets/javascripts/story.js b/assets/javascripts/story.js index 0779cfb097..3e1e8bfb97 100755 --- a/assets/javascripts/story.js +++ b/assets/javascripts/story.js @@ -143,6 +143,7 @@ RB.Story = RB.Object.create(RB.Model, { refresh: function(){ this.endEdit(); + this.unmarkSaving(); var sprint = this.$.parents('.sprint.backlog'); if(sprint.size()>0) sprint.data('this').recalcPoints(); }, @@ -195,7 +196,7 @@ RB.Story = RB.Object.create(RB.Model, { // Get the save directives. This should be overriden by descendant objects of RB.Story var saveDir = this.saveDirectives(); - $.ajax({ + RB.ajax({ type: "POST", url: saveDir.url, data: saveDir.data,