Created
March 6, 2011 19:07
-
-
Save Takazudo/857553 to your computer and use it in GitHub Desktop.
add factory method to jQuery UI's widget.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* === 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