将FullCalendar事件填充为数组 - 从另一个数组填充

时间:2012-02-06 22:16:25

标签: javascript jquery fullcalendar

我是JS的新手。我对Web服务的调用返回如下数组:

data {...}
    [0]: {...}
    [1]: {...}
    [2]: {...}
    [3]: {...}
    length: 4 

    data[0] {...}
    __type: "acme.acmeSystem.EventManagement.Event"
    Amenity: {...}
    DateFrom: "/Date(1326952800000)/"
    DateTo: "/Date(1326952800000)/"
    Description: "Friends coming over for a party."
    Food: false
    Id: 3
    IsPrivate: true
    Notes: ""
    NumberOfPeople: "Less than 10"
    Status: {...}
    TimeFrom: "8:30 AM"
    TimeTo: "11:30 AM"
    User: {...}

如何将其插入FullCalendar?

我正在尝试这样的事情:

GetEvents([], false,
    {
        successCallback: function (data) {
            if ((data) && (data != null) && (data.length > 0)) {

                buildingEvents = data;                  

                $('#calendar').fullCalendar({
                    theme: true,
                    header: {
                        left: 'prev,next today',
                        center: 'title',
                        right: 'month,agendaWeek,agendaDay'
                    },
                    editable: false,
                    events: buildingEvents
                });

            }

            data = null;
        }
    });

我需要从构建事件数组中创建另一个FullCalendar事件数组:

类似的东西:

$('#calendar').fullCalendar({
    events: [
        {
            title  : 'event1',
            start  : '2010-01-01'
        },
        {
            title  : 'event2',
            start  : '2010-01-05',
            end    : '2010-01-07'
        },
        {
            title  : 'event3',
            start  : '2010-01-09 12:30:00',
            allDay : false // will make the time show
        }
    ]
});

感谢。

1 个答案:

答案 0 :(得分:2)

嗯,我猜你可以使用jquery函数$.map()

  

jQuery.map(array,callback(elementOfArray,indexInArray))返回:数组
  描述:将数组或对象中的所有项目转换为新的项目数组。

基本上,您将从服务器接收的数据映射到fullCalendar可理解的EventData对象数组中:

var buildingEvents = $.map(data, function(item) {
    return {
        id: item.Id,
        title: item.Description,
        start: ... // "bind" all properties as needed
    };
});