2014-09-26 56 views
0

我已經創建了下面的兩個函數,從API獲取我需要的東西。但我無法弄清楚如何將它放到我的fullCalendar實例的'events'部分。如何將我的ajax請求連接到事件?

這是我的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);  
}; 

從例子中,我看到他們使用JSON給料從URL或XML文件,但我只需要在上面的函數中創建的javascript對象。

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

謝謝!

回答

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); 
     } 
    }); 
}; 
+0

漂亮!有效!謝謝! – SkyeBoniwell 2014-09-26 19:11:25