lbriquet lbriquet - 1 month ago 14
JSON Question

How to use event source with a JSON format other than the default

I'm using FullCalendar with an event source that I don't control. This source doesn't return a JSON exactly as FullCalendar need and the events are "one level down" where they should be.

For example, the JSON returned by the feed would be something like:

{
"sourcename": [
{
"start": "2016-04-18 14:00",
"end": "2016-04-18 18:00",
"title": "Working Group 1 meeting",
"className": "wg",
"url": "http://cnn.com/",
"type": "restricted"
},{
"start": "2016-04-18 14:00",
"end": "2016-04-18 18:00",
"title": "Working Group 2 meeting",
"className": "wg",
"url": "http://cbsnews.com/",
"type": "restricted"
}
]
}


Is there any option on FullCalendar that allows to specify the "path" on the returned JSON or any other form to use such a event source?

Answer

Since you have no control over the feed, I'm guessing events as a function is your only option.

This lets you control the request, but you need to send the parameters that would, otherwise, be appended automatically to the GET request (I'm talking about the data parameter).

On the AJAX request, just call the callback function (the 4th parameter on the function) with the events, which would be something like data.sourcename.

$('#calendar').fullCalendar({
    events: function(start, end, timezone, callback) {
        $.ajax({
            url: '/fullcalendar/events17',
            dataType: 'json',
            data: {
                start: start.format('YYYY-MM-DD'),
                end: end.format('YYYY-MM-DD')
            },
            success: function(data) {
                callback(data.sourcename);
            }
        });
    }
});