I want approval comments to wrap widgets in jQuery

When using jQueryUi widgets (dialog, datepicker, etc.), I always wonder if it would be better to wrap widgets in some kind of wrapper class. Here is one way to do this, but I'm not sure if this is a good practice.

Comment on

CODE

A small $ plugin that wraps a dialog box.

//Widget Wrapper plugin POC.
//Should be extended to support all Methods on all Widgets in jQueryUI   
(function ($) {
    $.fn.ww = function (uiWidgetName, options) {
    if (this.length == 1) {
        return new wrappers[uiWidgetName](this[0], options);
    }
}


var wrappers= {};

wrappers.dialog = function (element, options) {
    var theWidget = $(element);
    theWidget.dialog(options);

    this.open = function () {
        theWidget.dialog('open');
    }
    this.close = function () {
        theWidget.dialog('close');
    }
  }

} (jQuery))

It can be used as

var a = $("#WrapperTest").ww('dialog',{ autoOpen: false });            
a.open();
+3
source share
1 answer

Well, in fact, functions added to the namespace $.fnusing jQuery UI ( $.fn.dialog()in your case) are already wrappers around each widget method.

, - " " dialog(), . , , :

var dialogWidget = new $.ui.dialog({
    autoOpen: false
}, $("#WrapperTest")[0]);

, :

dialogWidget.open();
dialogWidget.close();
dialogWidget.option("autoOpen", !dialogWidget.option("autoOpen"));
// and so on.

"" ( $.fn.dialog()) , data, :

$("#WrapperTest").dialog({
    autoOpen: false
});

var dialogWidget = $("#WrapperTest").data("dialog");
dialogWidget.open();

.

: jQuery UI 1.9, data() , . :

var dialogWidget = $("#WrapperTest").data("ui-dialog");
dialogWidget.open();

- 1.9, , 1.10.

+1

Source: https://habr.com/ru/post/1785416/


All Articles