扩展jQuery插件的最佳途径 [英] Best Way to Extend a jQuery Plugin

查看:160
本文介绍了扩展jQuery插件的最佳途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相当新的jQuery用户,希望扩展现有的jQuery插件,占据我需要的大约75%。我试着在这个上做功课。我检查了以下问题在stackoverflow:



阅读扩展方法。然而,所有这些作业都让我感到困惑。我正在使用 fullcalendar 插件,需要修改一些行为以及添加新的事件挂钩。我坚持在插件封闭本身做这个?我错过了一些明显的东西吗?



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

解决方案

我试图扩展jQuery UI插件,这里是我找到的解决方案(通过jquery.ui.widget.js找到它):

 

(函数($){
/ **
*命名空间:插件位于
下的命名空间* pluginName:插件的名称
* /
var extensionMethods = {
/ *
*检索元素的ID
*这是现有插件中的一些上下文
* /
showId:function(){
返回this.element [0] .id;
}
};

$ .extend(true,$ [命名空间] [pluginName] .prototype,extensionMethods);


})(jQuery);

希望这有帮助,请问是否有任何问题。


I'm a fairly new jQuery user looking to extend an existing jQuery plugin that does about 75% of what I need. I've tried to do my homework on this. I've checked out the following questions on stackoverflow:

I've read up on the extend method. However, all of thise homework has left me confused. I'm working with the fullcalendar plugin and need to modify some of the behavior as well as add new event hooks. Am I stuck with doing this in the plugin closure itself? Am I missing something obvious?

Ideally we would be able to separate our code from the plugin code to allow for a possible upgrade. Any help would be greatly appreciated, especially pointers on where I'm missing some information or opinions on whether the solutions already presented in other Stack Overflow questions make sense. To me they contradict each other and I'm still left confused.

解决方案

I just had the same problem trying to extend jquery UI plugins, and here is the solution I found (found it through 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);

hope this helps, please ask if you have any questions.

这篇关于扩展jQuery插件的最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆