Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a function using namespace

Tags:

jquery

I have defined a JQfactory plugin like this

(function($, window, document) {
    $.jqfactory('my.tooltip', {

      /* My tooltip code */         

    }, false);
}(jQuery, window, document));

Now assuming I have included Bootstrap framework also in my page, and I want to call my version of tooltip() only, not the bootstrap version. How do I achieve using the namespace $.my.tooltip?

like image 697
spal Avatar asked Sep 27 '14 05:09

spal


1 Answers

As mentioned on the Bootstrap website:

http://getbootstrap.com/javascript/#js-noconflict

// return $.fn.tooltip to previously assigned value
var bootstrapTooltip = $.fn.tooltip.noConflict();

// give $().bootstrapBtn the Bootstrap functionality
$.fn.bootstrapTooltip = bootstrapTooltip;

You would use $.bootstrapTooltip to call the Bootstrap tooltip (if you ever want to use it). You'll be able to use $.tooltip for something else.

jQuery noConflict doesn't solve this issue, because it only separates the different jQuery versions. The tooltip function isn't part of the original jQuery code, so it doesn't make sense to use jQuery noConflict, unless you want to load 2 different jQuery versions and use 2 different jQuery objects "$" and "$$" for instance. This would definitely cause many problems later on because the developer will have a spaghetti code.

like image 129
Wissam El-Kik Avatar answered Oct 11 '22 13:10

Wissam El-Kik