I have a lightning component that pulls data from Salesforce and adds them as events on a calendar created using fullcalendar. The component helper contains the method fetching the data from Server. The component controller first pulls the data from Server and then creates the fullCalendar. This is the general structure of the code:
calendar.cmp
--------------------
<aura:component...>
<ltng:require scripts="/resource/jQuery,/resource/FullCalendar/js/fullcalendar.min.js" afterScriptsLoaded="{!c.afterScriptsLoaded}"/>
</aura>
calendarController.js
---------------------
afterScriptsLoaded: function(cmp,evt,helper){
var events = cmp.get("v.events");
if(events == null)
{
helper.fetchEvents(cmp);
}
helper.createCalendar(cmp,events);
}
calendarHelper.js
---------------------
fetchEvents: function(cmp){
var action = cmp.get("c.fetchFromServer");
action.setCallback(this, function(response){
var state = response.getState();
if(cmp.isValid() && state === "SUCCESS"){
cmp.set("v.events",response.getReturnValue());
}
});
},
createCalendar: function(cmp,events){
$('#calendar').fullCalendar({...});
}
I added the component to a lightning tab for testing. The first time I visit the tab, the component loads. But when I visit another tab and come back to this tab, the component is blank, even though the controller and helper methods are invoked in correct sequence and the data is pulled from the server too. This is because the calendar div is not visible yet when FullCalendar attempts to draw the calendar. I tried calling .fullCalendar('render') in rerender,afterRender method too, but that didn't help. However, when I reload the tab, the calendar is rendered correctly.
How can I ensure the data is rendered every time the calendar is accessed?
setTimeout($('#calendar').fullCalendar();, 3000);
.... try with settimeout.. to insure your all resource files load then after full calendar initializefullCalendar
properly loades.. you need to just increase set time out value.. I suggest first try 10,000( 10 second) after that try to reduce the value and use minimum valuecreateCalendar
after setting thev.events
.Since callback registered inaction.setCallback()
get executed asynchronously by aura framework,once the data is received from server.So,there's a less likely there will be values in when u docmp.get('v.events')
and give to fullcalendar increateCalendar