Jquery ui 从外部访问用于创建jQuery UI小部件的对象

Jquery ui 从外部访问用于创建jQuery UI小部件的对象,jquery-ui,Jquery Ui,我正在创建一个小部件,并希望为它提供一些值的默认值对象。我希望用户允许更改它们,然后正常创建小部件(因此将使用新的默认设置)。但用户可以通过什么方式访问默认值呢 例如: // separate file, create widget $.widget("namespace.myWidget", { defaults: { hello: "world" } } // index.html // then user should be able to do some

我正在创建一个小部件,并希望为它提供一些值的
默认值
对象。我希望用户允许更改它们,然后正常创建小部件(因此将使用新的默认设置)。但用户可以通过什么方式访问默认值呢

例如:

// separate file, create widget
$.widget("namespace.myWidget", {
    defaults: {
        hello: "world"
    }
}

// index.html
// then user should be able to do something similar
$.namespace.myWidget.defaults = { goodbye: "World" };
// or
$.namespace.myWidget.defaults.hello = "Dog";

// and then create widget normally
$(".selector").myWidget();

您可以将
默认值
对象直接附加到您定义的小部件:

(function ($) {
    $.widget("namespace.myWidget", {
        _init: function () {
            var options = $.extend({}, $.namespace.myWidget.defaults, this.options);
            /* Do something with options.hello */
        }
    });

    /* Create the `defaults` object */
    $.extend($.namespace.myWidget, {
        defaults: {
            hello: 'world'
        }
    });
}(jQuery));
然后小部件的用户可以修改默认值:

$.namespace.myWidget.defaults.hello = 'hello';

示例:

您可以将
默认值
对象直接附加到您定义的小部件:

(function ($) {
    $.widget("namespace.myWidget", {
        _init: function () {
            var options = $.extend({}, $.namespace.myWidget.defaults, this.options);
            /* Do something with options.hello */
        }
    });

    /* Create the `defaults` object */
    $.extend($.namespace.myWidget, {
        defaults: {
            hello: 'world'
        }
    });
}(jQuery));
然后小部件的用户可以修改默认值:

$.namespace.myWidget.defaults.hello = 'hello';
示例: