From f496f826696771db57aa21483be762969399fb98 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 26 Nov 2012 06:06:05 +0100 Subject: [PATCH] Added queueing support. --- .../app/lib/app_post/ajax.js.coffee | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/ajax.js.coffee b/app/assets/javascripts/app/lib/app_post/ajax.js.coffee index 9cc999857..df6efca59 100644 --- a/app/assets/javascripts/app/lib/app_post/ajax.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/ajax.js.coffee @@ -19,11 +19,16 @@ class _Singleton cache: false async: true - queue_list: {} + current_request: {} + queue_list: [] + queue_running: false count: 0 constructor: (@args) -> + # run queue + @_run() + # bindings $('body').bind( 'ajaxSend', => @_show_spinner() @@ -57,16 +62,28 @@ class _Singleton ) ) - ajax: (params, defaults) -> - data = $.extend({}, @defaults, defaults, params) + ajax: (params) -> + data = $.extend({}, @defaults, params ) if params['id'] - if @queue_list[ params['id'] ] - @queue_list[ params['id'] ].abort() - @queue_list[ params['id'] ] = $.ajax( data ) + if @current_request[ params['id'] ] + @current_request[ params['id'] ].abort() + @current_request[ params['id'] ] = $.ajax( data ) else - $.ajax( data ) + if params['queue'] + @queue_list.push data + if !@queue_running + @_run() + else + $.ajax(data) -# console.log('AJAX', params['url'] ) + _run: => + if @queue_list && @queue_list[0] + @queue_running = true + request = @queue_list.shift() + request.complete = => + @queue_running = false + @_run() + $.ajax( request ) _show_spinner: => @count++