如何将我的ajax请求连接到事件?

时间:2014-09-26 18:35:01

标签: jquery ajax fullcalendar

我已经创建了以下两个函数,从我的API中获取了我需要的函数。但我无法弄清楚如何将它放入我的fullCalendar实例的'events'部分。

这是我的ajax电话:

$.fn.CalendarEvents = (function () {
    return $.ajax("/api/exams/", {
        type: "GET",
        data: JSON.stringify(this),
        contentType: "application/json",
        success: function(data, status, XHR) {
            callEvents(data);
        }
    });
});

这是我的回调函数:

function callEvents(response) {
    var calObj = [];
    $.each(response, function (index, item) {
        var evt = {
            title: item.title,
            start: item.startDateTime,
            end: item.endDateTime
        };
        calObj.push(evt);
    });
    return calObj;
    //this writes out exactly what I need to go into my calendar
    console.log(calObj);    
};

从示例中,我看到他们使用来自URL或XML文件的JSON提要,但我只需要在上述函数中创建的javascript对象。

var calendar = $('#calendar').fullCalendar({
    events: {
        //What do I put here?
    }

谢谢!

1 个答案:

答案 0 :(得分:1)

根据文档events可以指定为函数:

var calendar = $('#calendar').fullCalendar({
    events: getCalendarEvents
...


var getCalendarEvents = function(start, end, timezone, callback) {
    $.ajax("/api/exams/", {
        type: "GET",
        data: JSON.stringify(this), // Not sure what you are trying to do here
        contentType: "application/json",
        success: function(response, status, XHR) {
            var calObj = [];
            $.each(response, function (index, item) {
                var evt = {
                    title: item.title,
                    start: item.startDateTime,
                    end: item.endDateTime
                };
                calObj.push(evt);
             });
            // You have to execute callback that is provided in the arguments
            // with your events data
            callback(calObj);
        }
    });
};