views:

132

answers:

2

Hello all,

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.

So far I've wrapped all my functions except "jQuery" function:

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?

A: 

If you're packaging JQury along with your code, you might as well just modify the library code to stick it somewhere useful.

To do it right, you'll need to replace all your Jquery.* calls with myadoon.jQuery...

timdev
no I'm using jQuery as a standalone file for easy updating.
Khaled Al Hourani
+2  A: 

You could wrap your add-on code inside an anonymous function, receiving and storing on that scope, the jQuery object:

(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...

CMS
the environment is unknown because every user who'll download my addon will have different other addons that might conflict mine. so is it worth it or not to use it (I just use the core Ajax functions no other plugins)?and one more thing what about "window.myaddonname" is it better to write window before it? because firefox's default namespace is "chrom" or something like that.
Khaled Al Hourani
@Khaled If `window` doesn't exist, then why not assign it to a global object (which you can reference via `this` from within your anonymous function).
kangax