seankenny / fullcalendar

Full-sized drag & drop event calendar (jQuery plugin)
http://arshaw.com/fullcalendar/
MIT License
60 stars 39 forks source link

Changing event source does not always update dom #35

Closed bulutcy closed 10 years ago

bulutcy commented 10 years ago

Hello, My Event object is something like: function CalendarEvent(booking) {

            this.id         = booking.id;
            this.title      = booking.label ? booking.label : '';
            this.start      = moment(booking.timeFrom, 'YYYY-MM-DD[T]HH:mm:ss.SSS').toDate();
            this.end        = moment(booking.timeTo,   'YYYY-MM-DD[T]HH:mm:ss.SSS').toDate();
            this.resourceId = booking.locationId;
            this.resources = [booking.locationId];

}

var eventSource1 = [ new CalendarEvent(booking1), new Calendar Event(booking2)]; var eventSource2 = [ new CalendarEvent(booking3), new Calendar Event(booking4)]; So my eventSource is = [ eventSource1 ]; If I remove eventSource1 and add eventSource2 and then remove eventSource2-add eventSource1 , events are not removed from dom. isSourcesEqual(src, source) returns false because my events are CalendarEvent but sources are plain object. I may post some examples if this is not clear.

seankenny commented 10 years ago

Hi - sorry for the delay. Is this an issue in the parent fork also? If you could provide a plunkr or jsfiddle with an example, is sure would help.

Thanks!

martijn80 commented 10 years ago

This is a bug in the original code, see : https://code.google.com/p/fullcalendar/issues/detail?id=2203

I fixed it by replacing lines 1517 - 1563 in your fork with the original fix :

            source = $.extend({}, sourceInput); // shallow copy
        }

        if (source) {

            // TODO: repeat code, same code for event classNames
            if (source.className) {
                if (typeof source.className === 'string') {
                    source.className = source.className.split(/\s+/);
                }
                // otherwise, assumed to be an array
            }
            else {
                source.className = [];
            }

            // for array sources, we convert to standard Event Objects up front
            if ($.isArray(source.events)) {
                source.origArray = source.events; // for removeEventSource
                source.events = $.map(source.events, function(eventInput) {
                    return buildEvent(eventInput, source);
                });
            }

            for (i=0; i<normalizers.length; i++) {
                normalizers[i].call(t, source);
            }

            return source;
        }
    }

    function removeEventSource(source) {
        sources = $.grep(sources, function(src) {
            return !isSourcesEqual(src, source);
        });
        // remove all client events from that source
        cache = $.grep(cache, function(e) {
            return !isSourcesEqual(e.source, source);
        });
        reportEvents(cache);
    }

    function isSourcesEqual(source1, source2) {
        return source1 && source2 && getSourcePrimitive(source1) == getSourcePrimitive(source2);
    }

    function getSourcePrimitive(source) {
        return (
            (typeof source === 'object') ? // a normalized event source?
                (source.origArray || source.url || source.events) : // get the primitive
                null
        ) ||
        source; // the given argument *is* the primitive
    }
seankenny commented 10 years ago

Great - really appreciate the help! I'll update with this shortly.