calendar icon indicating copy to clipboard operation
calendar copied to clipboard

How to load resources on-demand and sync it with events?

Open coirpower opened this issue 1 year ago • 1 comments

`

    // Embed resources directly using PHP variable
    var resources = @json($staffs);

    var calendarEl = document.getElementById('calendar');

    var calendar = new EventCalendar(calendarEl, {
        view: 'resourceTimelineWeek',
        firstDay: 1,
        slotDuration: {days: 1},
        headerToolbar: {
            start: 'prev,next today',
            center: 'title',
            end: 'resourceTimelineWeek'
        },
        eventSources: [
            {
                url: '/attendance/resourcesAndAttendance',
                method: 'GET',
                failure: function() {
                    alert('There was an error while fetching events!');
                }
            }
        ],
        views: {
            resourceTimelineWeek: {
                pointer: true, 
                resources: resources
            }
        },
        dayMaxEvents: true,
        nowIndicator: true,
        selectable: false            
    });
});

`

I use the above code but it looks like the calendar loads resources at initialization and does not automatically refresh them when I fetch events.

Basically, I have server side code that will return only the required resources and their events, and would like the calendar to fetch and load these dataset at runtime. Any suggestions?

coirpower avatar Nov 08 '24 02:11 coirpower

It seems that something like refetchResources() is needed. I will try to implement it in future versions.

vkurko avatar Nov 11 '24 13:11 vkurko

I wound up using a workaround:

        calendar.setOption('eventSources',
            [
                {
                    events: function (fetchInfo, successCallback, failureCallback) {
                        req = fetch(`/schedule/events?start=${fetchInfo.startStr}&end=${fetchInfo.endStr}`)
                        json = req.then(res => res.json())
                        json.then(j => {
                            window.history.pushState(null, '', `/schedule?start=${fetchInfo.startStr}&end=${fetchInfo.endStr}`);
                            calendar.setOption('resources', j.resources);
                            successCallback(j.events);
                        })

                        req.catch(e => failureCallback(e))
                    }
                }
            ]
        )

It's not perfect tho. Resources can shuffle around and collapsed resources open back up.

busti avatar Apr 13 '25 16:04 busti