add custom params to fullcalendar request

无人久伴 提交于 2019-12-01 09:04:32

问题


I'm want to add a parameter named foo to the request that fullcalendar submits when retrieving event data. According to the docs this can be achieved using:

var getFoo() = function() {
    // implementation omitted
};

$('#calendar').fullCalendar({

    loading: function(isLoading) {
        // CAN I SOMEHOW UPDATE foo FROM HERE?
    },
    events: {
        url: '/myfeed.php',
        type: 'POST',
        data: {
            foo: getFoo()
        }       
    }

});

I want the value of the foo parameter to be calculated each time the calendar event data is requested, but it seems that fullcalendar only calls getFoo() the first time it loads, then re-uses that value for each subsequent request.

I've tried using this the loading event that is triggered immediately before the data is loaded, but I can't figure out how to update the foo paramter from within this function.

Update

I followed the advice below and got it working with this:

        $('#calendar').fullCalendar({
            events: function(start, end, callback) {
                $.ajax({
                    url: 'getCalendarEvents',
                    dataType: 'json',
                    data: {

                        start: Math.round(start.getTime() / 1000),
                        end: Math.round(end.getTime() / 1000),
                        foo: getFoo()
                    },
                    success: function(doc) {
                        var events = eval(doc);
                        callback(events);
                    }
                });
            },
        });

回答1:


Maybe you could use the events as function to create your own ajax request.

var getFoo() = function() {
    // implementation omitted
};

$('#calendar').fullCalendar({
    events: function(start, end, callback) {
        var myFoo = getFoo();
        $.ajax({
            url: 'myxmlfeed.php',
            dataType: 'xml',
            data: {
                // our hypothetical feed requires UNIX timestamps
                start: Math.round(start.getTime() / 1000),
                end: Math.round(end.getTime() / 1000),
                foo: myFoo
            },
            success: function(doc) {
                var events = [];
                $(doc).find('event').each(function() {
                    events.push({
                        title: $(this).attr('title'),
                        start: $(this).attr('start') // will be parsed
                    });
                });
                callback(events);
            }
        });
    }
});



回答2:


Try setting lazyFetching to false. I'm guessing you're having this problem because fullCalendar tries to save you from unnecessary ajax requests.

Fullcalendar doc on lazyFetching



来源:https://stackoverflow.com/questions/7779643/add-custom-params-to-fullcalendar-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!