Skip to content

Instantly share code, notes, and snippets.

@thilo
Created September 25, 2011 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thilo/1240916 to your computer and use it in GitHub Desktop.
Save thilo/1240916 to your computer and use it in GitHub Desktop.
A Singleton UI Widget. I wonder if this is a sane approach?
// a ui dialog that can only appear one at a time uses jquery dialog
Widgets.UI = function() {
//gather the dom elements that made up the UI
var dialog = $('#dialog'),
linkTargetField = dialog.find("#link-target"),
saveBotton = dialog.find('#save'),
//changing state
node = undefined;
//event handler
saveBotton.click(function() {
node.attr('href', linkTargetField.val());
dialog.dialog('close')
});
// singleton to set state and open dialog.
return Widgets.UI || function($node) {
linkTargetField.val($node.attr('href'));
node = $node;
dialog.dialog({modal: true});
}
//calling initialization directly
}();
//usage
Widgets.UI($("#link"));
<div id="dialog" style="display:none">
<label for="link-target">Link URL</label>
<input type="text" id="link-target" placeholder="http://example.com/">
<input type="button" value="Done" id="save">
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment