2017-06-27 52 views
0

解決Fullcalendar eventSources不工作

這是我的整個Ajax響應:

{ 
    "data": [ 
     { 
      "id": 3, 
      "calendar_id": 1, 
      "title": "asdasd", 
      "start": "2017-06-20 14:06:00", 
      "end": "2017-06-20 16:06:00", 
      "allDay": 0, 
      "className": "bg-green", 
      "deleted_at": null 
     } 
    ] 
} 

定義:

$('#calendar').fullCalendar({ 
    slotDuration: //.. 
    minTime: //... 
    maxTime: //.. 
    defaultView: //... 
    defaultDate: //.. 
    header: { 
     //... 
    }, 
    eventSources: [ 
     { 
      url: '/api/calendar_events/' + id, 
      type: 'GET', 
      headers: { 
       'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
      }, 
      error: function() { 
       alert('there was an error while fetching events!'); 
      } 
     } 
    ], 
    eventLimit: //.. 
    selectable: //.. 
}); 

有什麼不對EventSources? fullcalendar已呈現,但未顯示任何事件。

注:我想使用eventSources而不是事件,因爲可以有多個源。

回答

0

這樣做:

eventSources: [ 
    { 
     events: function (start, end, timezone, callback) { 
      $.ajax({ 
       url: '/api/calendar_events/' + id, 
       type: 'GET', 
       headers: { 
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
       }, success: function (response) { 
        var events = []; 
        $(response['data']).each(function() { 
        events.push({ 
         id: $(this).attr('id'), 
         title: $(this).attr('title'), 
         start: $(this).attr('start'), 
         end: $(this).attr('end'), 
         className: $(this).attr('className'), 
         allDay : $(this).attr('allDay'), 
        }); 
       }); 
       callback(events); 
      } 
     }); 
    } 
    } 
],