javascriptjqueryjquery-uijquery-ui-widget-factory

jqueryui widget factory change method


I am writing a jqueryui widget, and I want to get at some widget data when the widget changes (using _trigger). I am using the widget as follows:

$("#myDiv").myWidget({
    change: function(e) {
        alert($("#filter").myWidget('getWidgetData'));
        // do something with the widget data.
    }
});

This code works (the alert fires and show the widget data when the widget calls _trigger). However, is this the correct/best way to get at the widget data inside the change callback ? For isntance, I can't seem to call this.getWidgetData, or e.getWidgetData.


Solution

  • You can attached your data to the change event you triggered. You should have something like :

    this._trigger('change', e);
    

    and could use :

    this._trigger('change', e, { widgetData: this.getWidgetData() });
    

    The _trigger function accepts A hash of data associated with the event. as its third arg as decribed in the documentation.

    And your code will become :

    $("#myDiv").myWidget({
        change: function(e, data) {
            alert(data.widgetData);
            // do something with the widget data.
        }
    });