2016-05-14 130 views
1

我试图在我的项目中完整日历中动态显示事件。有一个错误,我不明白这背后的原因,但是当我硬编码事件阵列它的工作正常。SyntaxError:missing;同时显示完整日历中的事件

日历jQuery代码:

$(document).ready(function() { 
    $('.fullcalendar').fullCalendar({ 
     header: { 
      left: 'prev,next today', 
      center: 'title', 
      right: 'month,basicWeek,basicDay' 
     }, 
     events: [ 
      eventList() 
     ] 
    }); 

Ajax调用

function eventList(){ 
    $.ajax({ 
     url :'/calendarMaintenanceData', 
     type: 'GET', 
     dataType: 'json', 
     success:function(data){ 
      $.each(data, function (index, item) {                      
       { 
        id:item.id, 
        title:item.machinecode, 
        start:item.estimated_maintenance_date 
       } 
      }); 
     } 
    }); 
} 

错误:

enter image description here

回答

1

eventList需要将所有事件放入数组中。您可以使用$.map而不是$.each来执行此操作。

success:function(data){ 
     return $.map(data, function (item) { 
      return {                      
       id:item.id, 
       title:item.machinecode, 
       start:item.estimated_maintenance_date 
      }; 
     }); 
    } 

但是,这仍然不会。问题是$.ajax是异步的,所以回调函数的返回值不会返回到fullCalender函数。创建数组后,您需要在回调函数中调用fullCalendar

function eventList(callback){ 
    $.ajax({ 
     url :'/calendarMaintenanceData', 
     type: 'GET', 
     dataType: 'json', 
     success:function(data){ 
      callback($.map(data, function (item) {                      
       return { 
        id:item.id, 
        title:item.machinecode, 
        start:item.estimated_maintenance_date 
       }; 
      }); 
     } 
    }); 
} 

$(document).ready(function() { 
    eventList(function(events) { 
     $('.fullcalendar').fullCalendar({ 
      header: { 
       left: 'prev,next today', 
       center: 'title', 
       right: 'month,basicWeek,basicDay' 
      }, 
      events: events 
     }); 
    }); 
}); 
+0

是的,它的工作很好......非常感谢Barmar。 – aniruddh

0

$.each()你有一个变量分配给这样

定义的对象
$.each(data, function (index, item) {                      
        var someobject = { 
         id:item.id, 
         title:item.machinecode, 
         start:item.estimated_maintenance_date 
         }; 
        }); 
+0

我在此之后新的错误显示 { 类型错误分配变量:d是未定义 \t ...形式(d)),a.extend(I,d)中,e &&(ⅰ。 source = e),i._id = d._id ||(void 0 === d.id?“_ fc”+ tb ++:d .... \t} – aniruddh

+0

您是否定义了变量d,因为它不是在你发布的代码 –

+0

不,我没有使用d变量任何地方 – aniruddh