简体   繁体   English

包装jQuery函数

[英]Wrap jQuery function

I'm developing a Firefox addon, after nominating the addon for public release in Firefox addons site, the reviewer asked me to wrap all my function inside a namespace or package name. 我正在开发Firefox插件,然后在Firefox插件网站上提名了公开发布的插件后,审阅者要求我将我的所有函数包装在名称空间或程序包名称中。

So far I've wrapped all my functions except "jQuery" function: 到目前为止,我已经包装了除“ jQuery”功能以外的所有功能:

myaddonname = {
    initialize: function() {
        var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

        if (document.getElementById("contentAreaContextMenu") != null) {
            document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", this.onContextMenuPopup(), false);
        }

        jQuery.noConflict();
    },

        .
        .
        .
        .
}

jQuery.noConflict and some other jQuery's Ajax calls are still unwrapped, any way to do that? jQuery.noConflict和其他一些jQuery的Ajax调用仍处于未包装状态,可以这样做吗?

You could wrap your add-on code inside an anonymous function, receiving and storing on that scope, the jQuery object: 您可以将附加代码包装在一个匿名函数中,以在该范围内接收和存储jQuery对象:

(function ($) {
  // $ is available inside this scope

  window.myaddonname = { // global add-on namespace
    initialize: function() {
      //...
    }
    //...
  };
})(jQuery.noConflict()); // execute and pass a reference to jQuery

And if you are on a very high-conflict environment, you could use jQuery.noConflict(true) (extreme mode), but use it carefully, with that option the window.jQuery object won't be registered, and most plugins won't work... 而且,如果您处于冲突非常严重的环境中,则可以使用jQuery.noConflict(true) (极端模式),但要谨慎使用,使用该选项时,将不会注册window.jQuery对象,并且大多数插件都不会工作...

If you're packaging jQuery along with your code, you might as well just modify the library code to stick it somewhere useful. 如果将jQuery和代码打包在一起,则最好修改库代码以将其粘贴在有用的地方。

To do it right , you'll need to replace all your jQuery.* calls with myadoon.jQuery... 正确执行此操作,您需要将所有jQuery。*调用替换为myadoon.jQuery ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM