Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Trying to access an array object to send the data to a view in Cakephp. I keep getting an "uncaught reference error, object undefined" in console. Here's the code:

        /* 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");
        }

    });
}

The arrayOfEvents is defined when a Scheduler drops a Person onto the calendar. It is transferring all of their information onto the arrayOfEvents object. I have a link for Schedulers to add all of the recently scheduled people to the database through an ajax call. It appears the function is not receiving the arrayOfEvents. Any ideas are welcome.

share|improve this question
    
Is your code complete? It seems it miss the first part. –  Uby Apr 28 '13 at 16:30
    
The first part just transfers the data from the users table to the calendar. I'm getting all the data I need into the arrayOfEvents. The console.log is showing it. –  Chris Apr 28 '13 at 16:35
1  
It looks like your arrayOfEvents is not in the global scope, is it inside some other handler? you can try by taking the line "var arrayOfEvents = [];" just above the function updateSchedule. –  Mohit Padalia Apr 28 '13 at 16:39
    
Unrelated: Your code will break in IE. You have a TRAILING COMMA OF DEATH in the events array. –  Joe Apr 28 '13 at 16:42
    
@sdespont That did it. If you want to put in an answer, I'll give you the credit. Thanks all! –  Chris Apr 28 '13 at 18:12

1 Answer 1

up vote 1 down vote accepted

The last }); before function updateSchedule shows us that the variable arrayOfEventsis not defined in the same scope. Move updateSchedule in the same scope, or the variable arrayOfEventsis outisde its current scope.

share|improve this answer
    
I had to move the function outside of the document ready and the variable declaration with it. –  Chris Apr 28 '13 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.