Extensibility: Extending init selectors to support application objects

Extensibility: Extending init selectors to support application objects


I'm working on a patch to jQuery to make it so that initialization can
support application specific objects through extensibility.
The use case is in applications where you have your own meaning for
certain parts of the system. For example, a Widget system. Your app has
a class "Widget" and you use these Widget objects a lot. The case here
is that you want $(widgetObject) to know how to extract the node for the
Widget object so that jQuery can manipulate it.
Currently I'm using jQuery.fn.init.callbacks for the callbacks, I'd like
to have some input on what API is actually desired.
An extremely simple example use:
function Widget(id) {
this.id = id;
}
jQuery.fn.init.callbacks.push(function( selector, context ) {
if( selector instanceof Widget ) {
return "#widget-"+ selector.id;
}
}):
jQuery(function($) {
var myWidget = new Widget('aaa');
$(myWidget).css('backgroundColor', 'red');
});
The current defined API is about the format of the callbacks:
* Callbacks return the selector and context passed to the jQuery object.
* Callback should check the selector and see if it is a object it wants
to mutate.
* If mutation is desired the callback should give a return value.
** Callback may return an array of dom nodes to be used...
** or, callback may return a string that will be used as a selector by
jQuery to find the dom nodes.
As you can see in the code above, it checked if the selector was a
Widget object, since it was it returned a string selector which jQuery
turned into a dom node. In this example the application just organized
it's Widgets in the Widget object with a setup where each Widget had an
id of the widget's id prefixed with "widget-".
So I'd like some input on what the desired API is. And any other ideas
of what other functionality people can come up with where application
objects should be handled. Currently I'm verifying that $(myWidget); and
$('<div/>').appendTo(myWidget); work.
Just to head off anyone suggesting workarounds. I don't post to the dev
list cause I'm interested in alternate less capable ways of doing
things, I post because there is functionality I'm interested in (and
most of the time, willing to help program the implementation for). I
already know you can hack your way around in your own Widget object to
try and create jQuery objects or whatever but that is not what I am
trying to do, I'm trying to make integration of jQuery into applications
easier.
For another note to anyone suggesting a proxy method (overriding
window.jQuery), that does not work. I can confirm that if you try that
technique this:
jQuery(function($) {
$(myWidget);
});
Will not work. And neither will this:
$('<div/>').appendTo(myWidget);
Because jQuery isolates itself inside of a closure to make sure that
overriding the jQuery method will not break jQuery on it's own from working.
--
~Daniel Friesen (Dantman, Nadir-Seen-Fire)