I am using fullcalendar.io
I dynamically populate my calendar, by adding events via evenSources. Such as:
$('.add-more-events').on('click', function(){
var myEventSourceUrl = // ... get the event source url somehow
var eventSourceColor = // ... get the color for this source
var eventSource = {
type: 'GET',
url: myEventSourceUrl,
color: eventSourceColor
};
$('.my-calendar').fullCalendar('addEventSource', eventSource);
}
Likewise, I dynamically unpopulate my calendar, by removing events via eventSource. Such as:
$('.remove-some-events').on('click', function(){
var myEventSourceUrl = // ... get the event source url somehow
var eventSourceColor = // ... get the color for this source
var eventSource = {
type: 'GET',
url: myEventSourceUrl,
color: eventSourceColor
};
$('.my-calendar').fullCalendar('removeEventSource', eventSource);
}
So far, so good: visually, events are appearing in the calendar (populated), & disappearing in the calendar (unpopulated) as expected. That is, whenever I click on the relevant button.
At some point, I want to reinitialize my calendar & redisplay the events that were last present in my calendar (keep the last eventSources I suppose).
Pseudo code would look like:
1. Back up my events
2. Destroy my calendar
3. Create & initialize new calendar
4. Add the backed up events (see step 1) to the new calendar
So I implemented the above such as:
var clientEvents = $('.my-calendar').fullCalendar('clientEvents'); /* [1] */
$('.my-calendar').fullCalendar('destroy'); /* [2] */
$('.my-calendar').fullCalendar(calendarOptions); /* [3] */
var arrayLengthClientEvents = clientEvents.length;
for (var i = 0; i < arrayLengthClientEvents; i++) {
$('.my-calendar').fullCalendar( 'addEventSource', clientEvents[i].source); /* [4] */
}
But for some reason, I get duplicated events when calling $('.my-calendar').fullCalendar('clientEvents')
.
Note: it seems to be happening when switching weeks, not sure about this though, it may be unrelated.
Resources:
- When using jquery fullCalendar, Why am i seeing duplicate events after switching months?
- http://fullcalendar.io/docs/event_data/Event_Source_Object/
- http://fullcalendar.io/docs/event_data/removeEventSource/
- http://fullcalendar.io/docs/event_data/clientEvents/
- filtering events with clientEvents method on fullCalendar
- JQuery Full Calendar - edit calendar view after initialization