Skip to content

Instantly share code, notes, and snippets.

@Takazudo
Created March 6, 2011 19:07
Show Gist options
  • Save Takazudo/857553 to your computer and use it in GitHub Desktop.
Save Takazudo/857553 to your computer and use it in GitHub Desktop.
add factory method to jQuery UI's widget.
/* === jQuery UI widget way === */
$.widget('ui.dashboard', {
_create: function(){
alert('hey widget was created!');
console.log(this.element); // this is what you attached.
}
});
$('#div1').dashboard(options);
/* ------------------------------------------------------------
problem: can't do like
var $dashboard = new $.ui.dashboard();
It's possible to do templating things in _create,
but you'll lost many widget functions if you did it.
then, do like following...
------------------------------------------------------------ */
/* === add static factory method === */
$.widget('ui.dashboard', {
_create: function(){
alert('hey widget was created!');
console.log(this.element); // this is what you attached.
}
});
$.ui.dashboard.create = function(options){
var src = '<div>or some html as string here</div>';
var $el = $(src); // or use templating library you like.
return $el.dashboard(options); // returns jQuery wrapped element
};
var $dashboard = $.ui.dashboard.create(options);
/* ------------------------------------------------------------
You'll get dashboard widgetified element. yey.
------------------------------------------------------------ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment