Accessing widget instance from outside widget

后端 未结 4 542
一生所求
一生所求 2020-12-25 12:16

This is a simple widget mock:

(function ($) {

    $.widget(\"ui.myDummyWidget\", {

        options: {
        },

        _create: function () {
        },         


        
4条回答
  •  不知归路
    2020-12-25 13:05

    The widget engine already does what you want: it calls data() internally to associate the widgets and their respective elements:

    $("#dummy").myDummyWidget();
    // Get associated widget.
    var widget = $("#dummy").data("myDummyWidget");
    // The following is equivalent to $("#dummy").myDummyWidget("hide")
    widget.hide();
    

    Update: From jQuery UI 1.9 onwards, the key becomes the widget's fully qualified name, with dashes instead of dots. Therefore, the code above becomes:

    // Get associated widget.
    var widget = $("#dummy").data("ui-myDummyWidget");
    

    Using unqualified names is still supported in 1.9 but is deprecated, and support will be dropped in 1.10.

提交回复
热议问题