尝试访问数组对象以将数据发送到 Cakephp 中的 View 。我不断在控制台中收到“ Uncaught ReferenceError ,对象未定义”。代码如下:
/* 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");
}
});
}
当 Scheduler 将 Person 放入日历时,就会定义 arrayOfEvents。它将所有信息传输到 arrayOfEvents 对象上。我有一个调度程序链接,可以通过 ajax 调用将所有最近安排的人员添加到数据库中。该函数似乎没有接收 arrayOfEvents。欢迎任何想法。
最佳答案
函数 updateSchedule
之前的最后一个 });
向我们表明变量 arrayOfEventsis
未在同一范围内定义。将 updateSchedule
移至同一范围内,或将变量 arrayOfEventsis
移至其当前范围之外。
关于Javascript 数组对象未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16265011/