( function ($) {
//
})(jQuery)
* This source code was highlighted with Source Code Highlighter .
$.widget
method. It takes 2 (or 3 in the case of inheritance) of the parameter. Officially, this method is called "widget factory"."my.myWidget"
. Namespace is required; nesting is not supported. The second parameter is an object literal, which, in fact, describes our widget:$.widget( "my.myWidget" , {
options: {
greetings: "Hello"
},
_create: function () {
this .element.html( this .options.greetings);
}
})
* This source code was highlighted with Source Code Highlighter .
_create
serves as a constructor, and will be called when the widget instance is created; This instance is indicated by this
.this.element
is the element on which the widget was hung. It is always a single item, not a collection (as is the case with regular plugins); if you hang a widget on a jQuery object that contains more than one element, then as many instances as there are elements will be created.options
field stores the default settings for the widget. This field is inherited, so it will always be in the widget, even if it is not explicitly declared.$.merge
method) with default settings before the _create
call.setOption
method is responsible for working with settings:$.widget( "my.myWidget" , {
options: {
greetings: "Hello"
},
_create: function () {
this ._render();
},
_render: function () {
this .element.html( this .options.greetings);
},
setOption: function (key, value) {
if (value != undefined) {
this .options[key] = value;
this ._render();
return this ;
}
else {
return this .options[key];
}
}
})
* This source code was highlighted with Source Code Highlighter .
var mw = $( '.mywidget' ).myWidget({greeting: 'Hi there!' })
console.log(mw.myWidget( 'option' , 'greeting' )); // 'Hi there!'
mw.myWidget( 'option' , 'greeting' , 'O HAI CAN I HAZ CHEEZBURGER?' );
* This source code was highlighted with Source Code Highlighter .
$.widget( "my.myWidget" , {
options: {
greetings: "Hello"
},
_create: function() {
this ._render();
},
_render: function() {
this .element.html( this .options.greetings);
},
sayHello: function(saying) {
alert(saying);
},
_setOption: function(key, value ) {
if (arguments.length == 1) {
this .options[key] = value ;
this ._render();
return this ;
}
else {
return this .options[key];
}
}
})
// …
mw.myWidget( "sayHello" , 42);
* This source code was highlighted with Source Code Highlighter .
$.widget( "my.myWidget" , {
options: {
greetings: "Hello"
},
_create: function () {
this ._render();
},
_render: function () {
this .element.html( this .options.greetings);
this ._trigger( "onAfterRender" , null , {theAnswer: 42})
}
})
// …
var mw = $( ".mywidget" ).myWidget(
{
greeting: "Hi there!" ,
onAfterRender: function (evt, data) {
console.log(data.theAnswer)
}
})
* This source code was highlighted with Source Code Highlighter .
.bind
in this form:mw.bind( 'onAfterRender.myWidget' , function (evt, data) {console.log(data.theAnswer)})
* This source code was highlighted with Source Code Highlighter .
$.Widget.prototype.destroy.call( this );
* This source code was highlighted with Source Code Highlighter .
$( '.dialog' ).dialog({
modal: true ,
closeOnEscape: false ,
// … ,
// …
})
* This source code was highlighted with Source Code Highlighter .
$.widget( "my.mydlg" , $.ui.dialog, {
options: {
modal: true ,
closeOnEscape: false ,
},
_create: function() {
$.ui.dialog.prototype._create.call( this );
}
})
* This source code was highlighted with Source Code Highlighter .
$.widget
method is a factory). Finishing the $ .widget method with a file, you can get widgets that themselves read their settings from the markup, themselves find vital elements for themselves, and are automatically organized into a hierarchical structure. But this is clearly a topic for a separate article.Source: https://habr.com/ru/post/120074/
All Articles