1

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/

3
  • It is converting the string to an object with each character becoming a property based on its index. What do you want it to produce? Over write anOption like this anOption: { a: "A" } Commented Oct 23, 2012 at 13:27
  • It looks like the widget options functionality works like this. var s = "a string"; console.log($.extend({}, s, {a: "new"}));​ Commented Oct 23, 2012 at 13:29
  • I also encoutered this problem in the jquery ui itself, namely the resizable widget: stackoverflow.com/questions/13028342/… Commented Oct 25, 2012 at 9:06

2 Answers 2

1

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);​
Sign up to request clarification or add additional context in comments.

2 Comments

I cant accept this solution, although thanks for giving me a possible workaround for now. Thing is, I cant control how other developers are using this widget, and would prefer being able to actually have all options work at first initialization.
Understood. I figured that I would post a possible way around it. Thanks for getting back to me with an explanation! I think that is good feedback!
0

This is a bug that has been fixed very recently:

http://bugs.jqueryui.com/ticket/8713

https://github.com/jquery/jquery-ui/commit/9b908878ae3a9c0fbbd9958b579f223a648c5c69

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.