创建后更新Fullcalendar事件?

时间:2014-05-03 19:35:31

标签: javascript jquery fullcalendar

当我创建一个新事件时,我使用tastypie REST API在服务器上发布它。创建时,服务器上的此事件具有新的ID,保存时生成:

select: function (startDate, endDate, allDay, jsEvent, view) {

    /* after selection user will be promted for enter title for event.*/

    var title = prompt('Event Title:');

    /*if title is enterd calendar will add title and event into fullCalendar. */

}
if (title) {
    calendar.fullCalendar('renderEvent', {
            title: title,
            start: startDate,
            end: endDate,
            allDay: allDay
        },
        true // make the event "stick"
    );
    $.ajax({
        url: 'http://localhost:8000/calendar/api/events/events/',
        dataType: 'json',
        contentType: 'application/json; encode=UTF-8',
        type: 'POST',
        data: JSON.stringify({
            title: title,
            start: startDate,
            end: endDate,
            allDay: allDay,
            user: "/calendar/api/events/users/{{ user.id }}/"
        }),
        success: function (data) {
            // Could i do something here?

        }
    });
    console.log({{user.id}},'{{ user}}')

}

},

我需要在客户端内存上更新事件,因为刚刚创建的事件有一个类似“fc_1”的事件ID或类似的东西,但我需要DB id,因为如果我立即拖动o调整事件的大小,我无法在服务器上更新它,因为它会尝试PUT此事件,但ID错误(如“fc_1”)。

1 个答案:

答案 0 :(得分:2)

在您的renderEvent对象中,您可以添加id:'pending'。然后在您的成功函数中,您可以使用fullcalendar clientEvents函数来获取事件数组,然后使用updateEvent添加新的id值:

success: function(data) {
    var newID = data.id;
    var events = calendar.fullCalendar('clientEvents');
    for(var i = 0; i < events.length; i++){
        if(events[i].id == 'pending') {
            events[i].id = newID;
            calendar.fullCalendar('updateEvent', events[i]);
        }
    }
}