尝试访问数组对象以将数据发送到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。任何想法都欢迎。
你的代码是否完整?它似乎错过了第一部分。 – Uby 2013-04-28 16:30:32
第一部分只是将用户表中的数据传输到日历。我将所有需要的数据都存入arrayOfEvents。 console.log显示它。 – Chris 2013-04-28 16:35:15
它看起来像你的arrayOfEvents不在全局范围内,它是否在一些其他处理程序?你可以尝试通过采取行“var arrayOfEvents = [];”就在函数updateSchedule上方。 – 2013-04-28 16:39:49