I'm working on a project using UI Widgets - they're really great.
I've run into a namespacing problem and I'd like to see what you think about my solution.
I'm building a survey interface, and want to use a 'survey' namespace. So I have a number of widgets like:
survey.core
survey.render
survey.form
etc.
This worked fine until I wanted to use survey.select - the problem is that the plugin will be called $.fn.select which is already taken, and I don't want to step on existing functions.
The documentation suggests using the ui namespace, but I'd rather not use a naming convention like ui.survey_core, ui.survey_select, etc. It seems like namespaces were intended to be better supported in the future but have not been yet.
Here's the solution I propose - that a $.fn[ namespace ] plugin is added as well such that you could call things like:
$('div').ui('droppable', options);
$('div').ui('droppable', 'option', 'tolerance', 'fit');
I'd suggest adding the plugin to $.fn[ name ] as well, but only if it is not already defined. So $('div').droppable() would still work, but a widget called ui.text would not overwrite $.fn.text.
Then, in my case, I could use "survey.select" and simply call $('div').survey('select', options) to instantiate.
I've modified my jquery.ui.widget.js so that $.widget.bridge() now takes 'namespace' as the first parameter, and does a simple mapping:
$.fn[ namespace ] = $.fn[ namespace ] || function(widgetName) {
var args = Array.prototype.slice.call( arguments, 1 );
return $.fn[ namespace ][ widgetName ].apply(this, args);
};
and instead of $.fn[ name ] the plugin is added to $.fn[ namespace ][ name ], and the simple name plugin is added only if it doesn't already exist:
$.fn[ name ] = $.fn[ name ] || $.fn[ namespace ][ name ];
Thoughts?
Thanks,
Jesse Skinner
www.thefutureoftheweb.com