vkurko / calendar

Full-sized drag & drop JavaScript event calendar with resource & timeline views

Home Page:https://vkurko.github.io/calendar/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Method addEvent bug

IndyHendrickx opened this issue · comments

I have encountered an issue with the addEvent method in my code. Here is the code snippet that I am using:

let data = [{ start: "2023-05-13 10:00", end: "2023-05-13 16:00", id: "1", isRegistered: "1", title: "Hello world" }, { start: "2023-05-11 12:00", end: "2023-05-11 13:00", id: "1", isRegistered: "0", title: "Hello world update?" }];

 // Add events if not already present
            for (var i = 0; i < data.length; i++) {
                // Check if event already exists
                if (calendar.getEventById(data[i].id)) {
                    // Update event by id
                    calendar.updateEvent({
                        start: data[i].start,
                        end: data[i].end,
                        resourceId: 1,
                        title: data[i].title,
                        color: "#779ECB",
                        id: data[i].id
                    })
                } else {
                    // Add new event
                    calendar.addEvent({
                        start: data[i].start,
                        end: data[i].end,
                        resourceId: 1,
                        title: data[i].title,
                        color: "#779ECB",
                        id: data[i].id
                    });
                }
            }
            console.log(calendar.getOption("events"));
            console.log(calendar.getEvents());

When I run this code, I expect the getEventById method to return the event that I just added with the ID '1' (when adding data index 1!). However, this is not working as expected. After adding all events to the calendar, I attempted to print the events for the calendar instance. However, calendar.getEvents() returns an empty array, even though the getOption("events") method shows that the events were added to the options.

It seems like the addEvent method is only adding the events to the DOM and the events property of the calendar instance. I appreciate any help or guidance on this issue. Thank you for the hard work put into this project, it is really impressive!

@IndyHendrickx Thanks for reporting the bug. This behavior is due to some optimizations, when the internal event storage is updated not in the main task, but in the microtask. I'll provide a fix a.s.a.p.

@IndyHendrickx Thanks for reporting the bug. This behavior is due to some optimizations, when the internal event storage is updated not in the main task, but in the microtask. I'll provide a fix a.s.a.p.

@vkurko thank you so much! In the main time I have worked around this problem, by writing this helper function:

function findEvent(calendar, id) {
    return calendar.getOption("events").find(o => o.id === id);
}

So take your time to provide a fix, coding in a hurry is never good! And again, thank you for all the hard work!

This should be fixed in v1.1.1. Please check.

I hope I can close this issue.