fullCalendar viewRender with ajax in infinite loop

153
December 02, 2017, at 1:26 PM

I am trying to set up a calendar in which the businessHours can vary throughout the year. For example, an ice cream store that is open from 12:00-10:00 P.M. from May 30th - October 1st every year. I have figured out how to pull the business hours from the server via an ajax call; but I want to make sure they update if the user moves between views in which the businessHours change. Using the example of the ice cream store again, if the user starts on the week before May 30th and then navigates to the week of May 30th, the businessHours should change.

To do this, I'm using jquery's $.post within the function for viewRender (as shown below)

viewRender: function(view, element){
               $.post("calendar_constraint_ajax.php", {start:    view.start.format(), end: view.end.format()}, function(json_bh) {
                if(json_bh != 'error')
                {
                    var bh = JSON.parse(json_bh);
                    $('#calendar').fullCalendar('option', {businessHours: bh, selectConstraint: "businessHours"});
                }
            });
        }

I get the change in business hours that I expect. But something repeatedly calls the server: view of Network in Chrome Developer Tools

When I don't include the AJAX call, this only seems to run once, but with the $.post, it seems to be stuck in an infinite loop.

I have two questions: 1.) Why does it keep calling the server? 2.) Is there a better way to update the businessHours property when the date range changes? (I'm still pretty new to fullCalendar.)

Answer 1

viewRender is firing multiple times because the event fires in several situations.

Here is the info regarding viewRender:

Callback will get triggered when the user changes the view, or when any of the date navigation methods are called.

So most likely you are calling it in another function you have - without realizing it.

You could try updating the hours using the eventAfterAllEvents to prevent repeated calls:

https://fullcalendar.io/docs/event_rendering/eventAfterAllRender/

READ ALSO
Toggle multiple images on multiple buttons\Divs inside main div

Toggle multiple images on multiple buttons\Divs inside main div

I have this code that toggle images inside divMy problem is when i try to toggle images in div-1 everything is ok but when im starting toggle images in div 2, images from div 1 dissapear and opposite way asswell

71
Using Javascript to make buttons that make new Javascript

Using Javascript to make buttons that make new Javascript

I am using Javascript to bring in a json list of races that have been completed from an external URLThere could be 1 race completed or a hundred

168
Viewing AJAX errors in Firefox Quantum

Viewing AJAX errors in Firefox Quantum

How can you view the details of the error in an AJAX call in Firefox QuantumI can't figure it out

190
euclidean distance from xyz coordinates in python

euclidean distance from xyz coordinates in python

I have a datafile that looks like this

158