扩展jQuery插件的最佳方法

jus*_*tkt 76 jquery plugins extend fullcalendar

我是一个相当新的jQuery用户,希望扩展现有的jQuery插件,它可以满足我所需要的75%.我试着做这个功课.我在stackoverflow上检查了以下问题:

我已经阅读了 extend方法.然而,所有这些功课让我感到困惑.我正在使用fullcalendar插件,需要修改一些行为以及添加新的事件挂钩.我是否坚持在插件封闭本身中这样做?我错过了一些明显的东西吗

理想情况下,我们可以将代码与插件代码分开,以便进行可能的升级.任何帮助将不胜感激,特别是关于我在哪里错过了关于其他Stack Overflow问题中已经提出的解决方案是否有意义的一些信息或意见的指示.对我来说,他们互相矛盾,我仍然感到困惑.

Jar*_*ott 85

我在尝试扩展jquery UI插件时遇到了同样的问题,这里是我找到的解决方案(通过jquery.ui.widget.js找到它):


(function($) {
/**
 *  Namespace: the namespace the plugin is located under
 *  pluginName: the name of the plugin
 */
    var extensionMethods = {
        /*
         * retrieve the id of the element
         * this is some context within the existing plugin
         */
        showId: function(){
            return this.element[0].id;
        }
    };

    $.extend(true, $[ Namespace ][ pluginName ].prototype, extensionMethods);


})(jQuery);

希望这有帮助,请询问您是否有任何疑问.

  • @Jared - 我最终在这里采纳你的建议来扩展jQuery UI自动完成.我在这里描述了我的目标:http://www.matthuggins.com/articles/extending-jquery-uis-autocomplete-to-improve-success,我在这里有完整的代码:https://github.com/mhuggins/ jquery-ui-autocomplete-hints.如果您有任何改进建议,我很乐意听到它们! (4认同)
  • 如何查找Namespace和pluginName? (4认同)
  • 所以澄清一下,如果我想扩展jQuery UI的自动完成,那么`Namespace`将是"ui"而``pluginName`将是"自动完成"? (2认同)

nep*_*jua 17

我有同样的问题来到这里,然后贾里德斯科特的回答激发了我的灵感.

(function($) {

    var fullCalendarOrg = $.fn.fullCalendar;

    $.fn.fullCalendar = function(options) {
        if(typeof options === "object") {
            options = $.extend(true, options, {
                // locale
                isRTL: false,
                firstDay: 1,
                // some more options
            });
        }

        var args = Array.prototype.slice.call(arguments,0);
        return fullCalendarOrg.apply(this, args);
    }

})(jQuery);
Run Code Online (Sandbox Code Playgroud)