在完整日历上切换月数时出错

时间:2014-03-11 02:34:46

标签: jquery fullcalendar

我遇到了fullcalendar的一个奇怪问题。当我点击< >按钮,相同的ajax动作执行的次数不止一次,如链接上的图像:http://snag.gy/VQfEU.jpg

如果我一直点击,会进行越来越多的ajax调用。我的代码是:

$(document).ready(function () {

        var sourceFullView = { url: '/Agenda/GetDiaryEvents/' };
        var sourceSummaryView = { url: '/Agenda/GetDiarySummary/' };
        var CalLoading = true;

        $('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            defaultView: 'month',
            editable: true,
            allDaySlot: false,
            selectable: true,
            slotMinutes: 15,

            events: '/Agenda/GetDiaryEvents/',

            eventClick: function (calEvent, jsEvent, view) {
                ClearPopupFormValues();
                $('#eventID').val(calEvent.id);
                $('#eventTitle').val(calEvent.title);
                $('#eventDate').val($.fullCalendar.formatDate(calEvent.start, 'dd/MM/yyyy'));
                $('#eventTime').val($.fullCalendar.formatDate(calEvent.start, 'HH:mm'));
                //Calculando timespan
                var start = new Date(calEvent.start.getFullYear(), calEvent.start.getMonth(), calEvent.start.getDate(), calEvent.start.getHours(), calEvent.start.getMinutes(), 0, 0);
                var end = new Date(calEvent.end.getFullYear(), calEvent.end.getMonth(), calEvent.end.getDate(), calEvent.end.getHours(), calEvent.end.getMinutes(), 0, 0);

                var weeks = 1000 * 60 * 60 * 24 * 7;
                var days = 1000 * 60 * 60 * 24;
                var hours = 1000 * 60 * 60;
                var mins = 1000 * 60;
                var secs = 1000;

                var diffWeeks = Math.ceil((start.getTime() - end.getTime()) / (weeks));
                var diffDays = Math.ceil((start.getTime() - end.getTime()) / (days));
                var diffHours = Math.ceil((start.getTime() - end.getTime()) / (hours));
                var diffMinutes = Math.ceil((start.getTime() - end.getTime()) / (mins));
                var diffSeconds = Math.ceil((start.getTime() - end.getTime()) / (secs));

                $('#eventDuration').val(Math.abs(diffMinutes));
                alert()
                $("#someKey").val(calEvent.someKey);
                $('#popupEventForm').modal('show');
                $('#eventTitle').focus();
                $('#btnPopupDelete').css('display', 'inline');
            },

            eventDrop: function (event, dayDelta, minuteDelta, allDay, revertFunc) {
                if (confirm("Confirma mudança no evento?")) {
                    UpdateEvent(event.id, event.start);
                }
                else {
                    revertFunc();
                }
            },

            eventResize: function (event, dayDelta, minuteDelta, revertFunc) {

                if (confirm("Confirma alteração na duração do evento?")) {
                    UpdateEvent(event.id, event.start, event.end);
                }
                else {
                    revertFunc();
                }
            },

            dayClick: function (date, allDay, jsEvent, view) {
                $('#descricao').val("");
                $('#eventDate').val($.fullCalendar.formatDate(date, 'dd/MM/yyyy'));
                $('#eventTime').val($.fullCalendar.formatDate(date, 'HH:mm'));

                ShowEventPopup(date);
                $('#btnPopupDelete').css('display', 'none');
            },

            eventRender: function (event, element) {
                element.find('.fc-event-time').text($.fullCalendar.formatDate(event.start, 'HH:mm'));
            },


            viewRender: function (view, element) {

                if (!CalLoading) {
                    if (view.name == 'month') {
                        $('#calendar').fullCalendar('removeEventSource', sourceFullView);
                        $('#calendar').fullCalendar('removeEvents');
                        $('#calendar').fullCalendar('addEventSource', sourceSummaryView);
                    }
                    else {
                        $('#calendar').fullCalendar('removeEventSource', sourceSummaryView);
                        $('#calendar').fullCalendar('removeEvents');
                        $('#calendar').fullCalendar('addEventSource', sourceFullView);
                    }
                }
            }

        });

        CalLoading = false;


    });

任何人都可以提供帮助吗?

由于

1 个答案:

答案 0 :(得分:0)

我在使用FullCalendar v2.1.1时遇到了同样的问题 lib中存在一个错误,源未被清除,因此每次点击都会继续增长 多次通话。作为解决方案,使用以下代码替换removeEventSource(source)

function removeEventSource(source) {
    sources = $.grep(sources, function(src) {
        return !isSourcesEqual(src, source);
    });
    // remove all client events from that source
    sources = [];  // this is a bug fix , clear source
    cache = $.grep(cache, function(e) {
        return !isSourcesEqual(e.source, source);
    });
    reportEvents(cache);
}