jQuery.widget( 'namespace.widgetname', namespace.superwidget, // - // {...} // , // ); jQuery.widget("demo.multi", {...}) jQuery.demo.multi and jQuery.demo.multi.prototype .draggable , droppable , etc. are inherited from the mouse plugin: jQuery.widget( "ui.draggable", $.ui.mouse, {...} ); jQuery.widget with a small w and jQuery.Widget with a large W).jQuery.Widget .jQuery.fn.multi . The .fn method serves as an interface between the DOM elements obtained in the jQuery object and the widget instances. A widget instance is created for each element in the jQuery object.$(":demo-multi") .jQuery.fnjQuery.widget.bridge$("#something").data("pluginname")this.element ) is available as an instance property of this.element , so it becomes very easy to interact with the plug-in and the element.$("#something").multi("refresh") - or directly through an instance - $("#something").data("multi").refresh() .this._trigger("clear") $( "#something" ).multi({ clear: function( event ) {} }); .bind : $( "#something" ).bind( "multiclear", function( event ) {} ); _destroy , etc.)options (note of the translator - the default options), _create _create , _setOption , and destroy . (function( $ ) { $.widget( "demo.multi", { // options: { clear: null }, // _create: function() { }, // _setOption _setOption: function( key, value ) { switch( key ) { case "clear": // break; } // jQuery UI 1.8, _setOption $.Widget.prototype._setOption.apply( this, arguments ); // jQuery UI 1.9 , _super this._super( "_setOption", key, value ); }, // destroy DOM, destroy: function() { // jQuery UI 1.8 destroy $.Widget.prototype.destroy.call( this ); // jQuery UI 1.9 _destroy } }); }( jQuery ) ); _create method. This will not duplicate the code in case of changes.<select multiple> , someone will want to run over the child <option> to create the corresponding <li> and <ul> . This can be implemented through the _create method: _create: function() { var self = this; this.list = $( "<ul>" ).insertAfter( this.element ); this.element.hide().find( "option" ).each(function( i, el ) { var $el = $( el ), text = $( el ).text(), item = $( "<li class='multi-option-item'>" + text + "</li>" ); item.appendTo( self.list ).click(function(){ console.log( $el.val() ); }); }); } _create this, it will create difficulties in defining the links between the original <option> element and the list items we create, or the problem when changing the state of <option> elements that were added or removed from the original <select> already after initializing the widget. Instead, we will make the refresh method, responsible for working with the element, and _create from the _create method. We will also move the processing of clicks on the elements of the list separately, and we will use event delegation so as not to hang new handlers after creating a new item. _create: function() { this.list = $( "<ul>" ) .insertAfter( this.element ) .delegate( "li.multi-option-item", "click", $.proxy( this._itemClick, this ) ); this.element.hide(); this.refresh(); }, refresh: function() { // this.items = this.items || $(); // , this.element.find( "option:not(.demo-multi-option)" ).each( $.proxy(function( i, el ) { // , var $el = $( el ).addClass( "demo-multi-option" ), text = $el.text(), // item = $( "<li class='multi-option-item'>" + text + "</li>" ) .data( "option.multi", el ) .appendTo( this.list ); // this.items = this.items.add( item ); },this)); // , this.items = this.items.filter( $.proxy(function( i, item ) { var isInOriginal = $.contains( this.element[0], $.data( item, "option.multi" ) ); if ( !isInOriginal ) { $( item ).remove(); } return isInOriginal; }, this )); }, _itemClick: function( event ) { console.log( $( event.target ).val() ); } $.fn $( "#something" ).multi( "_create" ) .data() you can call any of these methods directly: $( "#something" ).data( "multi" )._create() refresh indicative: since the user can control the elements of the select, we must ensure that he can update it. On the other hand, a service function for event handling, such as _itemClick , is required only for internal use and is not needed at all in the public interface of the plugin.this.element $( "#foo" ).myWidget() this.element will be a jQuery object containing the element with id foo . For multiple items for which .myWidget() is called, a separate instance of the plugin will be called for each item. In other words, this.element will always contain only one element.this.options$.demo.multi.prototype.options . Custom options overwrite default options.this.namespacethis.namethis.namespace , but in most cases also not used.this.widgetEventPrefixdialog has a close callback; when it is executed, the dialogclose event dialogclose . The name of the event consists of the event prefix and the name of the callback. By default, the value of widgetEventPrefix is the same as the name of the widget, but can be overwritten. For example, if a user started dragging an item, we do not want the draggablestart event to pop up, we want it to be called dragstart , for this we make the event prefix equal to "drag". If the name of the callback matches the event prefix, the event will be without a prefix. This will avoid events like dragdrag .this.widgetBaseClass element.addClass( this.widgetBaseClass + "-active" ) .addClass( "demo-multi-active" ) . The given example is more relevant for the factory itself and abstract plug-ins, such as $.ui.mouse ._create_init_init is called after _create . It can also be called at any time after the widget is created, in this case, _init can be used for re-initialization, without executing a wipe and re-creating.destroydestroy method. Including the removal of classes, events, the destruction of created elements, etc. This is the starting point for the destruction of your plugin, but for each plugin it is written individually, based on your needs.option.css() and .attr() methods. You can specify only a name to get a value, or a name together with a value, in order to set it, or to transfer an object, to set several values. The method calls _setOptions , so it should not be changed by third-party plugins._setOptions_setOptionoption method. This method can be changed in your plugin, if you need special behavior when changing certain options. For example, if the value of the window title changes in the dialog box, you need to run the title update. _setOption: function(key, value) { if (key === 'title') { this.titleElement.text(value); } $.Widget.prototype._setOption.apply(this, arguments); } _setOption method, we set the new value of the option. This should not be performed _setOption . Sometimes it is necessary to compare the old and the new values ​​in order to determine the correct behavior. You can compare this.options[key] with the value, since the call to the _setOption parent method is already at the very end. If you do not need to compare anything, you can make a call to the parent _setOption at the very beginning of the method.enableoption('disabled', false) . You can also catch the call of this helper by checking: if (key === "disabled") _setOption .disableoption('disabled', true) . You can also catch the call of this helper by checking: if (key === "disabled") _triggerthis.widgetEventPrefix above). You can also pass the event object that started the callback. For example, the drag event is triggered by a mousemove event, and it must be passed to _trigger . The third parameter is an object with data that is passed to the callback and event handlers. The data transmitted in this object should relate only to the current event, and should not be given to other methods of the plugin.Source: https://habr.com/ru/post/150549/
All Articles