From e875de7958531fbb1d4c10afb8271888f12bcfdc Mon Sep 17 00:00:00 2001 From: "D. Moraschi" Date: Fri, 14 Dec 2012 11:18:13 +0100 Subject: [PATCH] Update javascript/calendar_widget_init.js call applyMonthJson only when loadMonthJson ajax request ended --- javascript/calendar_widget_init.js | 73 ++++++++++++++++-------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/javascript/calendar_widget_init.js b/javascript/calendar_widget_init.js index 91f54b1..2b4fde7 100755 --- a/javascript/calendar_widget_init.js +++ b/javascript/calendar_widget_init.js @@ -2,16 +2,23 @@ $(function() { var calendar_url = $('.calendar-widget').data('url'); -var loaded_months = {}; +var loaded_months = {}, + finish = 0; -function loadMonthJson(month, year) { +function end(calendar) { + if(finish==3) setSelection(calendar); +} + + +function loadMonthJson(month, year, callback) { $.ajax({ url: calendar_url+"monthjson/"+year+month, - async: false, + async: true, dataType: 'json', success: function(data) { json = data; - loaded_months[year+month] = data; + loaded_months[year+month] = data; + if(callback) callback(); } }); } @@ -22,7 +29,6 @@ function applyMonthJson(month, year) { for(date in json) { if(json[date].events.length) { $('[data-date="'+date+'"]').addClass('hasEvent').attr('title', json[date].events.join("\n")); - } } } @@ -64,14 +70,19 @@ $('.calendar-widget').each(function() { var json; m = calendar.pad(month); if(!loaded_months[year+m]) { - loadMonthJson(m, year); + loadMonthJson(m, year, function() { + applyMonthJson(m, year); + setSelection(calendar); + }); + } + else { + applyMonthJson(m, year); + setSelection(calendar); } - json = loaded_months[year+m]; - applyMonthJson(m, year); - setSelection(calendar); }, - onInit: function(calendar) { + onInit: function(calendar) + { previous = calendar.getPrevMonthYear(); next = calendar.getNextMonthYear(); @@ -82,26 +93,23 @@ $('.calendar-widget').each(function() { prev_year = previous[1]; next_year = next[1]; - loadMonthJson( - this_month, - this_year - ); - - loadMonthJson( - prev_month, - prev_year - ); - - loadMonthJson( - next_month, - next_year - ); - applyMonthJson(this_month, this_year); - applyMonthJson(prev_month, prev_year); - applyMonthJson(next_month, next_year); - - setSelection(calendar); - + loadMonthJson(this_month, this_year, function() { + applyMonthJson(this_month, this_year); + finish++; + end(calendar); + }); + + loadMonthJson(prev_month, prev_year, function() { + applyMonthJson(prev_month, prev_year); + finish++; + end(calendar); + }); + + loadMonthJson(next_month, next_year, function() { + applyMonthJson(next_month, next_year); + finish++; + end(calendar); + }); } }; @@ -110,12 +118,11 @@ $('.calendar-widget').each(function() { var parts = $(this).data('start').split('-'); opts.month = Number(parts[1])-1; opts.year = parts[0]; - - } + $(this).CalendarWidget(opts); }) }); -})(jQuery); \ No newline at end of file +})(jQuery);