I've made a question on the forum about a issue I was facing in my application. Although the question was marked as duplicate, I tried to use the good practices to solve the problem. After very tries my code finnaly works, but not in the way I expected. I'm making an application using the Fullcalendar plugin. In the "eventRender" callback, which is executed to each event on the calendar, the callback I've made is making hundreds of ajax calls :@ I think I don't understand very well all that callback thing. Here's my code:
$().ready(function() {
var obterDadosCategorias = (function() {
var dadosCategorias;
var efetuarPost = function(callback){
$.post(
"{{ baseRoute }}/cadastro/categoria/listar"
, {
"ajax": "true"
}
).done(function(data) {
var obj = $.parseJSON(data);
if (obj.result) {
callback(obj.data);
} else {
alert('Erro interno: não foi possível obter os dados das categorias');
}
}).fail(function(){
alert('Erro interno: não foi possível obter os dados das categorias');
});
};
return function(callback) {
if (dadosCategorias) {
callback(dadosCategorias);
return;
}
efetuarPost(function(dados) {
dadosCategorias = dados;
callback(dados);
});
};
})();
$("#fullcalendar").fullCalendar({
"eventRender": function(event, element, view) {
if (view.name === "month") {
var beforeMonth = parseInt(event.end.format("YYYYMMDD")) < parseInt(view.intervalStart.format("YYYYMMDD"));
var afterMonth = parseInt(event.start.format("YYYYMMDD")) > parseInt(view.intervalEnd.clone().subtract(1, 'days').format("YYYYMMDD"));
if (beforeMonth || afterMonth) {
return false;
}
}
obterDadosCategorias(function(dadosCategorias) {
$(element).css("background-color", dadosCategorias["id_" + event.categoria].cor).css("color", textoBrancoOuPreto(dadosCategorias["id_" + event.categoria].cor));
});
return $(element);
}
});
});
Hope you guys can help me. :D Thanks in advance.