0

I'm using the $.getScript to load a jQuery plugin in my own plugin. The problem is that I can only access the loaded plugin and instantiate an object in the callback function of the $.getScript method.

I would like to know if there is a way to do the instantiation outside of the callback function. See the actual implementation in the following code:

init = function( options ) {
    $.getScript('javascript/lib/fullcalendar-1.5.3/fullcalendar/fullcalendar.js', function() {
        // Instantiation is require to be done here
        self.fullCalendar(options);
    });

    self.opt = $.extend(true, {}, defaults, options);
    self.data('settings', self.opt);
    if (typeof options.test === 'function') {
        // I would liked to do this here but at this point fullcalendar is not defined
        self.fullcalendar(options);

        var tests = test.call(self, 3, 1);
        options.test.call(self, tests, null);
    }
}
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
JDL
  • 231
  • 4
  • 16

1 Answers1

0

Create a global variable and assign the calendar to it whenever your initiate you fullCalendar.

init = function( options ) {
    var myCalendar;

    $.getScript('javascript/lib/fullcalendar-1.5.3/fullcalendar/fullcalendar.js', function() {
                        // Instantiation is require to be done here
        myCalendar = self.fullCalendar(options);
    });

    // Test full Calendar
    var viewName = myCalendar.fullCalendar('getView').name;
    alert(viewName);
}

I haven't tried this. But this is just a main idea. I hope this helps.

Adil Malik
  • 6,279
  • 7
  • 48
  • 77
  • Thanks for your reply, I've already tried this solution but it didn't worked. Variable myCalendar is always undefined outside $.getScript method. What I'm trying to do is to access a variable referencing the return object of self.fullCalendar(options) outside the init function but I'm getting out of ideas on how to achieve this. – JDL Jun 29 '12 at 18:02
  • Ok, l've got it to work finally but without using $.getScript. The problem was from the $.getScript function. As the asyn option is set to true by default, self.fullCalendar(options) is always null. I've used the conventional $.ajax by setting asyn to true and it's worked great. – JDL Jun 29 '12 at 19:13
  • I think the rest of your init function runs while the getScript takes a little longer time. Since it has to get some script. Any ways the good thing is that you have it finally working the other way. Good Luck !! – Adil Malik Jun 29 '12 at 19:18