postCreate Fires Multiple times in stand alone editor

postCreate Fires Multiple times in stand alone editor

Rob BrunRob Brun Posts: 56Questions: 2Answers: 14

Hello, I am currently trying to utilized a stand alone editor in conjunction with the full Calendar library https://fullcalendar.io/. I have it so where the full Calendar dayclick event fires my editor and allows the user to enter a new event into the calendar. On my postCreate event I grab what is returned from the server and create an object that is added into the calenders eventSource.

The problem is the postcreate event fires multiple times on consecutive entries, if its the second consecutive entry it will fire twice if it's the third it will fire three times. I think that I need to unbind something some where. I was wondering if anybody can point me in the right direction. This is what the day click property looks like in the calendar object

   dayClick: function (date, jsEvent, view) {
                var selectedDate = date.format("MM/DD/YYYY hh:mm a");
              
                editor.on('initCreate', function (e) {
                    editor.field("EventStart").val(selectedDate);
                    editor.field("EventEnd").val(selectedDate);
                });

                var justTheDate = moment(selectedDate, "MM/DD/YYYY hh:mm a").format("MM/DD/YYYY");
                editor.title("<h4 class='modal-title'>Create a New Event for " + justTheDate + "<h4>");
                editor.buttons("Create New Event");
                editor.create();

                editor.on('postCreate', function (e, json, data) {
                    var id = data["EventId"];
                    var title = data["EventTitle"];
                    var isFullDay;

                    if (data["IsFullDay"] === 0) {
                        isFullDay = false;
                    } else {
                        isFullDay = true;
                    }

                    var eventStart = moment(data["EventStart"]).format("YYYY-MM-DDTHH:mm:ssZ");
                    var eventEnd = moment(data["EventEnd"]).format("YYYY-MM-DDTHH:mm:ssZ");
                    var eventColor = data["EventColor"];
                    var url = data["Url"];

                    var returnObject = {
                        id: id,
                        title: title,
                        start: eventStart,
                        end: eventEnd,
                        allDay: isFullDay,
                        color: eventColor,
                        url: url
                    };

                    eventSource.push(returnObject);
                    $('#calendar').fullCalendar('renderEvent', returnObject, 'stick');

                });


            },

Thank you for your help

Shay

This question has an accepted answers - jump to answer

Answers

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14
    Answer ✓

    Ok, so I solved it. Pulling the postCreate out of the dayClick event cleared up the issue. I have the initCreate in the dayClick event because I need the data from that event to use there and there seems to be no issue I have noticed yet. Sorry about the spam.

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Good to hear you've got it working.

    Another option would be to use one() rather than on() which would attach an event for only the next triggering.

    However if you were to take that route you need to be careful to remove the event (off()) should the edit be cancelled, otherwise it would trigger twice the next time!

    The solution you have now is probably the best one.

    Allan

This discussion has been closed.