The eventReceive callback runs when an external event is dropped on the calendar and has been rendered. When this happens we can use the "clientEvents" method to check how many events are already present on the day the external event was dropped on, and decide on that basis whether to remove it from the calendar.
eventReceive: function(event) {
var newEventDay = event.start.startOf('day');
var existingEvents = $("#calendar").fullCalendar("clientEvents", function(evt) {
//this callback will run once for each existing event on the current calendar view
//if the event has the same start date as the new event, include it in the returned list (to be counted)
if (evt.start.startOf('day').isSame(newEventDay)) {
return true;
} else {
return false;
}
});
//if this new event means there are now more than 2 events on that day, remove this new event again (N.B. we must do it like this because by this point the event has already been rendered on the calendar)
if (existingEvents.length > 2) $("#calendar").fullCalendar("removeEvents", function(evt) {
if (evt == event) return true;
});
}
N.B. This assumes that the events you're dragging don't span more than one day. If they can, you'll need to alter this code a bit and bring the end date into the equation as well.
You can see a working example here: http://jsfiddle.net/Lfm1odm1/2/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…