【问题标题】:Accessing loaded script using jQuery getScript method使用 jQuery getScript 方法访问加载的脚本
【发布时间】:2012-06-29 14:56:29
【问题描述】:

我正在使用$.getScript 在我自己的插件中加载一个 jQuery 插件。问题是我只能访问加载的插件并在$.getScript方法的回调函数中实例化一个对象。

我想知道是否有办法在回调函数之外进行实例化。具体实现见以下代码:

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);
    }
}

【问题讨论】:

    标签: jquery fullcalendar getscript


    【解决方案1】:

    创建一个全局变量并在您启动 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);
    }
    

    这个我没试过。但这只是一个主要想法。我希望这会有所帮助。

    【讨论】:

    • 感谢您的回复,我已经尝试过此解决方案,但没有成功。变量 myCalendar 在 $.getScript 方法之外始终未定义。我想要做的是访问一个引用 self.fullCalendar(options) 的返回对象的变量在 init 函数之外,但我对如何实现这一点没有想法。
    • 好的,我终于让它工作了,但没有使用 $.getScript。问题出在 $.getScript 函数上。由于 asyn 选项默认设置为 true,因此 self.fullCalendar(options) 始终为空。我通过将 asyn 设置为 true 来使用传统的 $.ajax,效果很好。
    • 我认为您的 init 函数的其余部分会运行,而 getScript 需要更长的时间。因为它必须得到一些脚本。任何方式的好处是你最终让它以另一种方式工作。祝你好运!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多