How to access functions in jQuery plugins from outside the plugin?

Take, for example, the form plugin:

jQuery.autocomplete = function(input, options) { function abc(){} function def(){} } 

Functions work fine and as expected. However, I want to be able to call the abc () function; from $ (document) .ready (function () {}); expression. The following does not work, and I would really like some advice.

 $(document).ready(function() { jQuery.autocomplete.abc(); abc(); $(this)->abc(); } 
+4
source share
4 answers

Well, when you entered into the field, the jQuery plugin added divs to the tag cloud. Clicking on one of these β€œtags” adds it to the secondary list. Clicking on the secondary list removed the item from the secondary list. Then I wanted this click to add an item to the main list only if it matches the text entered in the field. For this reason, I again wanted to run the autocomplete code. To get around this, I added a secondary function to my personal code with the "click" event, and not with the "keydown". This allowed me to add a trigger ("click") to the div in the secondary list, which again triggered the autocomplete action. The problem is solved. I could not understand this without any help. Thanks!

0
source

When the init plugin has this ...

 if( !data ) { /* Settings Extraction */ $.extend(s, defaults, options); /* Initialize Code */ /* Data Storage */ var YoPlugin = $('<div />', { text : $this.attr('title') }); $(this).data('YoPlugin', { target : $this, myInternalFunction: myInternalFunction, settings : $.extend({}, s), YoPlugin : YoPlugin }); data = $this.data('YoPlugin'); } 

You can open internal functions, as myInternalFunction has demonstrated. How to get into an object from an event called $ ('body') will exit 'this' as a body, so ...

 var multiSel = $('.YoPlugin'); var singleSel = multiSel[0]; //or other way to select the singleton or specific plugin enhanced target var pluginDataObj = $(singleSel).data('YoPlugin'); var func = pluginDataObj['myInternalFunction']; func(); 

I believe that adding a link as an external plugin link is better i.e. like init: declared in the plugin or similar routes via $ .fn.YoPlugin.myInternalFunction

In any case, this set of fragments provides R&D night to explore and help understand what lir bir betta is.

Also, you definitely need to read everything that you can absorb here ...

http://alexsexton.com/blog/2010/02/using-inheritance-patterns-to-organize-large-jquery-applications/

+4
source

Your autocomplete function should explicitly make these functions available, possibly by placing links to them on some global object. Otherwise, they are completely hidden from the outside world and cannot be accessed.

0
source

In private jQuery functions (for example, those that you described in the question) are usually attached to the instance. Inside a function, they use a local scope variable or the this to interact with the instance. So, I do not see how to call them a call from onload. If the plugin was developed correctly, the author will implement callbacks wherever necessary; callbacks will be assigned through the instance, however, not globally.

If you are writing this plugin and want this behavior, the syntax might look something like this:

 jQuery.autocomplete = function(input, options) {} jQuery.autocomplete.abc = function(){}; jQuery.autocomplete.def = function(){}; 

These features will be available worldwide through $.autocomplete .

0
source

All Articles