Fullcalendar refetch events

asp.net-mvc, fullcalendar, jquery

Solution

In the interests of future readers, here is the correct solution for FullCalendar v2.

events: function (start, end, timezone, callback) {
    $.ajax({
        url: '/Calendar/GetEvents/',
        dataType: 'json',
        data: {
            start: Math.round(start.getTime() / 1000),
            end: Math.round(end.getTime() / 1000),
            projectId: $("#ProjectId option:selected").val(),
            storeId: $("#StoreId option:selected").val(),
            jobId: $("#JobId option:selected").val(),
            orderType: $("#OrderType option:selected").val(),
            shippingId: $("#ShippingId option:selected").val()
        }
        success: function(data) {
            callback(data)
        }
    });

Problem

I need to re-fetch events for full calendar not only on calendar buttons, but on my own drop-down changes, because they define the criteria for events search. The only way I could pass values of dropdowns so far by using the `$.ajax`, but now the events don't get to the calendar. I'm missing something, as I don't fully seem to understand the functionality. ``` $(function () { InitializeCalendar(); $("#ProjectId").change(function () { $('#calendar').fullCalendar('refetchEvents'); }); $("#JobId").change(function () { $('#calendar').fullCalendar('refetchEvents'); }); $("#StoreId").change(function () { $('#calendar').fullCalendar('refetchEvents'); }); $("#OrderType").change(function () { $('#calendar').fullCalendar('refetchEvents'); }); $("#ShippingId").change(function () { $('#calendar').fullCalendar('refetchEvents'); }); }); function InitializeCalendar() { $('#calendar').fullCalendar({ header: { left: 'prev,next today', center: 'title', right: 'month,agendaWeek,agendaDay' }, events: function (start, end) { $.ajax({ url: '/Calendar/GetEvents/', dataType: 'json', data: { start: Math.round(start.getTime() / 1000), end: Math.round(end.getTime() / 1000), projectId: $("#ProjectId option:selected").val(), storeId: $("#StoreId option:selected").val(), jobId: $("#JobId option:selected").val(), orderType: $("#OrderType option:selected").val(), shippingId: $("#ShippingId option:selected").val() } **SOMETHING MISSING HERE** //this puts calendar into infinite loop //$('#calendar').fullCalendar('refetchEvents'); }); } }); } ``` Controller: ``` public JsonResult GetEvents(double start, double end, int? projectId, int? storeId, string orderType, int? shippingId, int? jobId) { // Some code... IList<OrderEvent> events = orderDTOs.Select(orderDTO => new OrderEvent { id = orderDTO.OrderId, title = orderDTO.OrderId.ToString(), start = ToUnixTimespan(orderDTO.CreatedDate), end = ToUnixTimespan(orderDTO.CreatedDate.AddHours(1)), url = "/Order/Search" }).ToList(); return Json(events, JsonRequestBehavior.AllowGet); } ```

Original source