Accessing widget instance from outside widget

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

This is a simple widget mock:

(function ($) {

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

        options: {
        },

        _create: function () {
        },         


        
4条回答
  •  盖世英雄少女心
    2020-12-25 13:05

    I'm not sure in which version it was introduced but, if all you wish to do is calling a widget's method you can use this:

    $("#myWidget").myDummyWidget("hide");
    

    Where myWidget is the id of the DOM element holding an instance of your widget. This will call the hide method.

    If the method you need to call needs parameters you can pass them after the method name, like this:

    $("#myWidget").myDummyWidget("setSpecialAnswer",42);
    

    Also, you can look for all instances of your widget using the special selector :widgetName and call methods on them. The following code snippet will call the hide method on all myDummyWidget widgets.

    $(":ui-myDummyWidget").myDummyWidget("hide");
    

    Mind that the widget fullname is composed of a prefix ("ui" in the example) and the widget's name ("myDummyWidget") separated by a score ("-").

    You should use your own prefix for your custom widgets; "ui" is generally reserved for jQueryUI pre-built widgets.

    I hope that helps. :)

提交回复
热议问题