2013-05-10 33 views
1

我遇到了FullCalendar問題,我一直在尋找解決方案,但沒有取得任何成功。 我使用eventClick打開當前事件數據的疊加窗體。一切都很好,直到我改變主意,不想編輯這個事件,而是另一個。這導致ajax發送請求2次,一次用於打開的事件(準備編輯但表單未提交),一次用於真正編輯和提交的事件。事件對象在完整日曆中保存了以前的事件屬性

$('#sc-calendar').fullCalendar({ 
    eventClick: function(event) { 
     //opening overlay form window 
       $('#submit-event-update').bind('click',function() { //click submit 
       $.ajax({ 
        type: "GET", 
        url: "event_update", 
        data: "id="+event.id+"&title="+event.title+"&start="+event.start+"&end="+event.end, 
        cache: false, 
        success: function() { 
        $('#submit-event-update').unbind(); 
        $('#sc-calendar').fullCalendar('updateEvent',event); 
        } 
       }); 
       }); 
      } 
     }); 

這開始是我的噩夢。請幫忙!

回答

0

在我看來,onclick事件監聽器對於#submit-event-update存在問題。

您應該修改代碼以這樣的方式

$('#sc-calendar').fullCalendar({ 
    eventClick: function(event) { 

    //opening overlay form window 

    //Assign the event id of this event to the global variable event_id 
    event_id = event.id; 
    } 
}); 

$('#submit-event-update').bind('click',function() { //click submit 
    $.ajax({ 
     type: "GET", 
     url: "event_update", 
     data: "id=" + event_id + ..., //Note how event.id is not used anymore 
     cache: false, 
     success: function() { 
     $('#sc-calendar').fullCalendar('updateEvent',event); 
     } 
    }); 
}); 

我改變了它,讓你的onclick事件處理程序綁定到該按鈕只有一次,而不是每一個被點擊的事件時間。我還分配了一個變量event_id,它保存當前事件ID的值。

現在,作出解釋。你說:

一切工作的很好,直到我改變主意,不想編輯 這個事件,但另一個。

當你點擊一個事件會發生什麼?

您將onclick事件綁定到#submit-event-update。現在,如果你點擊按鈕,那麼你將進入你的AJAX調用的success()回調,然後解除綁定按鈕。但是如果你改變了主意並且不點擊提交按鈕呢?現在,您有一個onclick偵聽器,舊數據已綁定到該按鈕。當你選擇另一個事件時,你有兩個事件監聽器綁定到同一個按鈕,因此,AJAX請求發送兩次

閱讀JavaScript如何處理事件綁定here可能是值得的。

+0

這是相當不錯的解決方案。但是,如果我有另一個屬性,我在窗體中更改它不會更改日曆或日曆初始化。 – mesnicka 2013-05-13 12:06:22