Skip to content

Commit

Permalink
Update javascript/calendar_widget_init.js
Browse files Browse the repository at this point in the history
call applyMonthJson only when loadMonthJson ajax request ended
  • Loading branch information
D. Moraschi committed Dec 14, 2012
1 parent 2d8a0ba commit e875de7
Showing 1 changed file with 40 additions and 33 deletions.
73 changes: 40 additions & 33 deletions javascript/calendar_widget_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
}
Expand All @@ -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"));

}
}
}
Expand Down Expand Up @@ -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();

Expand All @@ -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);
});
}

};
Expand All @@ -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);
})(jQuery);

0 comments on commit e875de7

Please sign in to comment.