Wrap jQuery function
-
08-07-2019 - |
Question
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?
Solution
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...
OTHER TIPS
If you're packaging jQuery 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...