Skip to content

Instantly share code, notes, and snippets.

@meleyal
Created February 17, 2011 11:17
Show Gist options
  • Save meleyal/831529 to your computer and use it in GitHub Desktop.
Save meleyal/831529 to your computer and use it in GitHub Desktop.
jQuery $.widget.bridge template
// http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/
var Widget = function(options, element){
this.options = options;
this.element = element;
this._init();
}
Widget.prototype = {
// called only once
_create: function(){
console.log('create widget!');
// create code
},
// called each time widget is called without arguments
_init: function(){
console.log('init widget!');
// init code
},
option: function( key, value ){
// get/change options AFTER initialization:
// you don't have to support all these cases,
// but here's how:
// signature: $('#foo').bar({ cool:false });
if( $.isPlainObject( key ) ){
this.options = $.extend(true, this.options, key);
// signature: $('#foo').option('cool'); - getter
} else if ( key && typeof value === "undefined" ){
return this.options[ key ];
// signature: $('#foo').bar('option', 'baz', false);
} else {
this.options[ key ] = value;
}
return this; // make sure to return the instance!
},
publicFn: function(){ // notice no underscore
console.log('publicFn');
},
_privateFn: function(){
console.log('privateFn');
}
};
$.widget.bridge("foo", Widget);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment