3

I have data held in the options variable, but in an event (OnChange) of a select list this refers to the DOMElement and therefore this.options refers to the list of objects.

everywhere else I can have

var o = this.options;

but this is no use in this context.

How to get/set the widget information ?

3 Answers 3

3

Use the this._on() method to bind the handler. This method is provided by the jQuery UI widget factory and will make sure that within the handler function, this always refers to the widget instance.

_create: function () {
    ...
    this._on(this.selectElement, {
        change: "_onChange" // Note: function name must be passed as a string!
    });
},
_onChange: function (event) {
    // 'this' is now the widget instance.
},
Sign up to request clarification or add additional context in comments.

2 Comments

changed answer to this as origianal answer (from myself) has been superceeded.
If you're extending a widget with a custom name then the event name will be: [my custom widget name in lower case][event name] So if you're extending sortable to mySortable and want to bind on the update event, it will be: "mysortableupdate" : "_onChange"
1

You can also simply do this in the event handler (e.g. for the autocomplete widget)

select: function(event, ui) {
   var options = jQuery(event.target).data("autocomplete").options;

   ...
}

1 Comment

jQuery(ev.target).data("autocomplete") is undefined, for: jQuery(ev.target).data("autocomplete").options;
-1
(function($){
    $.widget(
        "ui.my_ui_widget", 
        {
            //------------------------------------------------------------------
            _init : function(){
                var o = this.options; // 2
                o.context = this;
                ...
                // 3
                $('#'+o.id).bind('change', {context:this}, this.on_change );
                ...
            },
            //------------------------------------------------------------------
            on_change: function(event) {
                var context = event.data.context; // 4
            var o       = context.options;

                o.id = ''; // 5

                var sel = this; // event handler context
                var selectedIndex = sel.selectedIndex;
                if (selectedIndex < 0) return;

                o.id = sel.options[selectedIndex].value; // 5

                context.update_ui(); // 6
            },
            //------------------------------------------------------------------
        } 
    );
    //--------------------------------------------------------------------------
    $.extend( $.ui.my_ui_widget, {
        getter       : "id",
        getterSetter : "xxx, yyy",

        defaults : {
               ...
             context   : undefined, // 1
             ...
             on_change : undefined,
               ...
            }
        }
    );
    //--------------------------------------------------------------------------
})(jQuery);

$(document).ready(function() {
    $('.my_ui_widget').each( function(i) {
            var id = this.id;
            var mode = '';

            try{
                mode = this.attributes['mode'].value;
            } catch(err) {
                mode = 'unlinked';
            }

            $('#'+id).my_ui_widget( {id:id, mode:mode} );
        }
    );
});
  1. Enable context from the beginning
  2. Remember the widget's this as context (or self if preferred)
  3. Pass the context in the jquery way to event handler
  4. Extract when needed on the handler side
  5. Access the widget data
  6. Call widget methods as needed

1 Comment

ev.data is undefined, for: var context = ev.data.context; //4

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.