Hatcham
Hatcham

Reputation: 330

fullcalendar 2.0 event chaining

I am just in the process of upgrading fullcalendar from 1.6 to 2.0.

Some code that used to work, now no longer does. For example:

$('#calendar').fullCalendar('changeView', 'agendaDay').fullCalendar('gotoDate', date);

Works in 1.6, but in 2.0 I now get an error:

TypeError: $(...).fullCalendar(...) is undefined

I can work around it by doing:

$('#calendar').fullCalendar('changeView', 'agendaDay');
$('#calendar').fullCalendar('gotoDate', date);

But this is not really an acceptable solution for me. I am saving various calendar vars (date, view, etc) in the session, and because the above results in two ajax calls at almost the exact same time, the session vars are not being stored in a reliable way.

Anyone have any idea why this might be, and how I might get around it? Is fullcalendar not returning an instance of itself perhaps?

Many thanks in advance,

Mark

Upvotes: 0

Views: 392

Answers (1)

Jarzyn
Jarzyn

Reputation: 290

As I've figured it out by reading through the sources, in version 2.x.x it is not possible to chain method calls.

Let me show you why:

this is the JQuery plugin method, and when the method is called, response object turns into called method response.

$.fn.fullCalendar = function(options) {
    var args = Array.prototype.slice.call(arguments, 1); // for a possible method call
    var res = this; // what this function will return (this jQuery object by default)

    this.each(function(i, _element) { // loop each DOM element involved
        var element = $(_element);
        var calendar = element.data('fullCalendar'); // get the existing calendar object (if any)
        var singleRes; // the returned value of this single method call

        // a method call
        if (typeof options === 'string') {
            if (calendar && $.isFunction(calendar[options])) {
                singleRes = calendar[options].apply(calendar, args);
                if (!i) {
                    res = singleRes; // record the first method call result
                }
                if (options === 'destroy') { // for the destroy method, must remove Calendar object data
                    element.removeData('fullCalendar');
                }
            }
        }
        // a new calendar initialization
        else if (!calendar) { // don't initialize twice
            calendar = new Calendar(element, options);
            element.data('fullCalendar', calendar);
            calendar.render();
        }
    });

    return res;
};

Both changeView and gotoDate methods do not return anything, so 'undefined' is returned.

// a method call
        if (typeof options === 'string') {
            if (calendar && $.isFunction(calendar[options])) {
                singleRes = calendar[options].apply(calendar, args);
                if (!i) {
                    res = singleRes; // record the first method call result

The simplest solution is just to replace

 return res;

in the file fullcalendar.js in the method given above for

return this;

But that's definetely not the best solution, as any upgrade will remove this "fix".

Upvotes: 2

Related Questions