Avoid repeating options when combining jQuery widgets

84 Views Asked by At

I have created a Datetime widget based on the widget factory, similar to the jQuery datepicker, that has a set of options. For example you can set the localized months (month names in different locals).

I want to create an Interval widget (datepicker with from - to) that will be composed of two Datetime widgets. My problem is on how I can avoid repeating the default options of the inner widget to the outer widget. Of course, the two inner widgets will be passed the same set of options.

The datetime widgets:

$(function () {
    $.widget("Foo.Datetime", {
        options: {
            months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "etc"]
        }           
        // other stuff here
    }
    });
});

How can I allow passing options from the Interval widget to the Datetime widgets without having to redefine them inside the interval widget?

The obvious way is the following, witch I want to avoid:

$(function (){
    $.widget("Foo.Interval", {
        options: {
            months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "etc"]
        },

        _create: function(){
            var d = this.element.find(".Datetime");
            d.Datetime({
                months: this.options.months
            });
        }
        // handle _setOption accordingly 
    })
});
0

There are 0 best solutions below