Search code examples
javascriptfullcalendarfullcalendar-schedulerfullcalendar-4

eventDrop, oldResource and newResource in fullcalendar scheduler v4


I'm trying to eventually create a php script to update mysql. As I go step by step and try to set the variables and have js behave correctly, I'm having difficulty with changing the 'resource' rooms in the scheduler.

If I change the location of the event and change the room, I get a JS alert saying "Title was dropped on to [new time] from [old time]. Old room [oldresource]. New Room [newresource]". That's working well.

However, if I move the event to a location on the same day, I get errors - because info.oldResource and info.newResource are only available IF the event has moved to a NEW resource. If they're moving within the same resource, the object doesn't exist.

I tried working in an IF statement. Console Log shows null, but my if statement is not stopping the processing of the rest of the code. This will eventually (I think) result in the commands not being run correctly - once I pass them to PHP.

I plan on having an 'else' statement that does not include oldResource or newResource dialogue to process changes that stay within the same resource.

My code is as such:

    eventDrop: function(info) {
if (typeof info.oldResource !=="null")   {
    var oll = console.log(info.oldResource);
    var nww = console.log(info.newResource);
    var oldroom = (info.oldResource.id);
    var newroom = (info.newResource.id);
    var newtitle = info.event.title;
    /*if (oldroom = newroom) {alert ("Same Room");}*/
alert(newtitle + " was dropped on " + info.event.start.toString() + ".Old Time: " + info.oldEvent.start.toString() + ". From: " + oldroom + ". To: " + newroom );

    if (!confirm("Are you sure about this change?")) {
    info.revert();
    }}
},

Solution

  • You don't need typeof here. Just write

    if (info.oldResource != null) 
    

    instead.

    P.s. if you call typeof on a property that's set to null I would expect the typeof call to return "object", not "null". But like I said, it's irrelevant because you don't need it. Just check for the value null directly.