overriding a jquery ui widget string option with a

2019-08-13 13:02发布

I want to have a jquery ui widget option be a string by default, but I want it to be able to overridden by an object. When I do this, I actually get the string converted to an object in some strange way, and then extended with whatever object I pass in.

$.widget("ui.test", {

    options: {
        anOption: "a,b,c"
    },

    _create: function() {
        console.log(this.options);
    }

});

$('div').test({
    anOption: {
        a: 'A'
    }
});

If I skip passing in the option to the widget, it will be received as a string in the _create method. If I pass in an object, the strange behavior occurrs. In chromes js console log I get this, which is not what I want.

Object
anOption: Object
0: "a"
1: ","
2: "b"
3: ","
4: "c"
a: "A"

How do I solve this?

jsfiddle: http://jsfiddle.net/MatteS75/s9wK2/

2条回答
Luminary・发光体
2楼-- · 2019-08-13 13:18

If you change the option after the widget is created, it will overwrite the option with the object that you want. Is this acceptable?

$.widget("ui.test", {

    options: {
        anOption: "a string"
    },

    _create: function() {
        console.log(this.options.anOption);
    }

});

$('div').test({});


$('div').test("option", "anOption", {
    a: "option1",
    b: "option2"
});

var anOption = $('div').test("option", "anOption");

​console.log(anOption);​
查看更多
登录 后发表回答