如何避免fullcalendar上的事件重复?

时间:2016-01-14 01:49:38

标签: jquery events duplicates fullcalendar

我正在尝试使用完整日历在同一天避免事件重复。 我有一个名为“已阻止”的事件,如果某个特定日期已经有阻止事件,则不允许用户添加另一个事件。

我的问题是,如何在客户端的特定日期获取事件列表?

这是我的代码:

$(document).ready(function () {
$('.calendar').fullCalendar({
      dayClick: function (date, jsEvent, view, resourceObj) {
        // Here I would like to check if this date already have a 'Blocked' event, if yes do not need to render another event and make another ajax call.
        var newEvent = {
          title: 'Blocked',
          start: date
        };
        $('.calendar').fullCalendar('renderEvent', newEvent, 'stick');
        $.ajax({
          type: "GET",
          url: "block_date",
          dataType: "json",
          data: {date: date.toJSON()},
          error: function (result) {
            $('.calendar').fullCalendar('removeEvents', newEvent);
          }
        });
      },
      eventClick: function (calEvent, jsEvent, view) {
        if (calEvent.title == 'Blocked') {
          $('.calendar').fullCalendar('removeEvents', calEvent._id);
          $.ajax({
            type: "GET",
            url: "unblock_date",
            dataType: "json",
            data: {date: calEvent.start.toJSON()},
            error: function (result) {
              $('.calendar').fullCalendar('renderEvent', calEvent);
            }
          });
        }
      }
    }
);

});

1 个答案:

答案 0 :(得分:0)

找到我的问题的答案。不确定这是否是处理它的最佳方式,但它现在正在运作。

dayClick: function (date, jsEvent, view) {
        var blockedEvents = $('.calendar').fullCalendar('clientEvents', function (event) {
          return event.title == 'Blocked' && event.start.format() == date.format();
        });
        if (blockedEvents.length < 1) {
          var newEvent = {
            title: 'Blocked',
            start: date
          };
          $('.calendar').fullCalendar('renderEvent', newEvent, 'stick');
          $.ajax({
            type: "GET",
            url: "block_date",
            dataType: "json",
            data: {date: date.toJSON()},
            error: function (result) {
              $('.calendar').fullCalendar('removeEvents', newEvent);
            }
          });
        } else {
          $('.calendar').fullCalendar('removeEvents', blockedEvents[0]._id);
          $.ajax({
            type: "GET",
            url: "unblock_date",
            dataType: "json",
            data: {date: blockedEvents[0].start.toJSON()},
            error: function (result) {
              $('.calendar').fullCalendar('renderEvent', blockedEvents[0]);
            }
          });
        }
      },