Javascript数组对象未定义

时间:2013-04-28 16:28:06

标签: javascript jquery cakephp

尝试访问数组对象以将数据发送到Cakephp中的视图。我在控制台中不断收到“未捕获的引用错误,对象未定义”。这是代码:

        /* 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对象。我有一个Scheduler链接,可以通过ajax调用将所有最近安排的人员添加到数据库中。看来函数没有收到arrayOfEvents。欢迎任何想法。

1 个答案:

答案 0 :(得分:1)

函数});之前的最后一个updateSchedule向我们显示变量arrayOfEventsis未在同一范围内定义。移动updateSchedule在同一范围内,或变量arrayOfEventsis超出其当前范围。