For training, I make a plugin that opens / closes pop-up windows. Accordingly, it should have methods: open a window, close a window, etc. These methods should be called, both inside the plugin, when performing certain actions, as well as externally: $('#box').plugin.close() ΠΈΠ»ΠΈ $('#box').plugin('close');
I re-read all manuals, possible structures of plug-ins. Now it is implemented using the standard structure, when the methods init
, destroy
are allocated to the methods
object. But I do not understand how to properly implement the close
method.
I do not understand what parameters it needs to pass, or how to call it, so that it sees the variables defined in the body of the plug-in function.
Here is an example structure:
(function( $ ){ var pop = function(element, options, callback1st, callback2nd) { var elem = $(element), obj = this, self = $(this); var settings = $.extend({ openSpeed: '300', closeSpeed:'300', openAnim: 'default', closeAnim: 'default', method: 'default', checkboxShow: '3', callback1st: 'default', callback2nd: 'default' }, options || {} ); var mpControl = elem.find(".mpcontrol"), mpContent = elem.find("div.mpcontent"); //Π»ΠΎΠ³ΠΈΠΊΠ° ΡΠ°Π±ΠΎΡΡ } var methods = { init:function() {}, close:function() {} } $.fn.milkpop = function( method, callback1st, callback2nd ) { //Π²ΡΠ·ΡΠ²Π°Π΅ΠΌ ΠΌΠ΅ΡΠΎΠ΄ ΠΈΠ»ΠΈ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΠ΅ΠΌ ΠΏΠ»Π°Π³ΠΈΠ½ }; })( jQuery );
I need the mpControl and mpContent variables for a particular instance to be available inside the Close () method
How to implement it correctly? So that the method can be called inside and outside the plug-in and interact with all its variables.
Update From what came up after writing the post.
Write control elements in
data
for the element to which the plugin is applied. Next, inside the method, pull out these objects and work with them (I donβt really like it)Write control objects to the
settings
array. Pass it to theClose()
function, since He is still there to choose the speed and effect. There read from it.var settings = $.extend({ controlEl: elem.find(".mpcontrol"), contentEl: elem.find("div.mpcontent") }, options || {} ); methods.close.call(elem, settings);
Everything is already working, it remains to get confidence that this is the right approach :)