2013-04-28 123 views
1

嘗試訪問數組對象以將數據發送到Cakephp中的視圖。我在控制檯中不斷收到「未捕獲的參考錯誤,對象未定義」。代碼如下:Javascript Array對象undefined

 /* initialize the calendar 
    -----------------------------------------------------------------*/ 
    var arrayOfEvents = []; 
    $('#calendar').fullCalendar({ 
     header: { 
      left: 'prev,next today', 
      center: 'title', 
      right: 'agendaWeek,agendaDay' 
     }, 
     defaultView: 'agendaWeek', 
     editable: true, 
     events: [ 
      <?php foreach ($users as $user) { foreach ($user['Event'] as $event): ?> 
      { 
       start: '<?php echo $event['start_date']; ?>', 
       end: '<?php echo $event['end_date']; ?>', 
       title: '<?php echo $event['title']; ?>', 
       allDay: false, 
       color: '#077169' 
      }, 
      <?php endforeach; } ?> 
     ], 
     droppable: true, // this allows things to be dropped onto the calendar !!! 
     drop: function(date, allDay) { // this function is called when something is dropped 

      // retrieve the dropped element's stored Event Object 
      var originalEventObject = $(this).data('eventObject'); 
      // we need to copy it, so that multiple events don't have a reference to the same object 
      var copiedEventObject = $.extend({}, originalEventObject); 
      // assign it the date that was reported 
      copiedEventObject.start = date; 
      copiedEventObject.end = (date.getTime() + 3600000)/1000; // default shift length is 1 hour 
      copiedEventObject.userId = originalEventObject.userId; 
      copiedEventObject.allDay = false; 
      // render the event on the calendar 
      // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/) 
      $('#calendar').fullCalendar('renderEvent', copiedEventObject, true); 
      // Push events into array 
    --->  arrayOfEvents.push(copiedEventObject); 
      //todo: the calendar needs to insert events into the event calendar. 
      console.log(arrayOfEvents); 
     } 
    }); 
}); 

function updateSchedule() 
{ 
    ---> var data = "numOfEvents=" + arrayOfEvents.length; 


    // You can get all events out of the array here 
    for (var i = 0; i < arrayOfEvents.length; i++) { 
     var event = arrayOfEvents[i]; 
     data += "&id" + i + "=" + event.id 
       + "&start" + i + "=" + event.start 
       + "&end" + i + "=" + event.end; 
    } 

    // Make your ajax post here 
    $.ajax({ 
     type: "POST", 
     url: "<?php echo $this->webroot; ?>events/add", 
     data: data, 
     success: function(response) { 
      alert('done!'); 
     }, 
     fail: function(response) { 
      alert("error"); 
     } 

    }); 
} 

arrayOfEvents是在Scheduler將一個Person拖放到日曆上時定義的。它將所有的信息傳送到arrayOfEvents對象上。我有一個調度程序的鏈接,通過ajax調用將所有最近調度的人員添加到數據庫。看來該函數沒有收到arrayOfEvents。任何想法都歡迎。

+0

你的代碼是否完整?它似乎錯過了第一部分。 – Uby 2013-04-28 16:30:32

+0

第一部分只是將用戶表中的數據傳輸到日曆。我將所有需要的數據都存入arrayOfEvents。 console.log顯示它。 – Chris 2013-04-28 16:35:15

+1

它看起來像你的arrayOfEvents不在全局範圍內,它是否在一些其他處理程序?你可以嘗試通過採取行「var arrayOfEvents = [];」就在函數updateSchedule上方。 – 2013-04-28 16:39:49

回答

1

函數updateSchedule之前的最後一個});告訴我們變量arrayOfEventsis未在相同範圍內定義。在同一範圍內移動updateSchedule,或者變量arrayOfEventsis超出其當前範圍。

+0

我不得不將文檔移動到文檔之外,並使用它的變量聲明。 – Chris 2013-04-28 21:03:16