(function($){
$.widget(
"ui.my_ui_widget",
{
_init : function(){
var o = this.options;
o.context = this;
...
$('#'+o.id).bind('change', {context:this}, this.on_change );
...
},
on_change: function(event) {
var context = event.data.context;
var o = context.options;
o.id = '';
var sel = this;
var selectedIndex = sel.selectedIndex;
if (selectedIndex < 0) return;
o.id = sel.options[selectedIndex].value;
context.update_ui();
},
}
);
$.extend( $.ui.my_ui_widget, {
getter : "id",
getterSetter : "xxx, yyy",
defaults : {
...
context : undefined,
...
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} );
}
);
});
- Include context from the start
- Remember the widget as context (or "I" if you want)
- Pass context in jQuery for event handler
- Extract if necessary on the processor side
- Access widget data
- Call widget methods if necessary
source
share