Side navigation
#8020 closed feature (patchwelcome)
Opened January 20, 2011 09:02AM UTC
Closed February 02, 2011 12:17PM UTC
Native namespace support in selector methods
Reported by: | marc@smartsite.nl | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | 1.next |
Component: | core | Version: | 1.5b1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
John Resig has worked on a namespace solution (http://bugs.jquery.com/~john/plugins/subclass/ - dead link now) that seems to have worked in 1.3, but stopped working due to core changes in 1.4.
The idea is that jQuery should support grouping plugin selector methods in namespaces, so that you could call:
$('div.myclass').myNs.doSomething();
This way, the risk of method name collisions is reduced.
Attachments (0)
Change History (5)
Changed January 20, 2011 02:53PM UTC by comment:1
component: | unfiled → core |
---|---|
priority: | undecided → low |
Changed January 20, 2011 03:51PM UTC by comment:2
It's true that Ariel Flesler has written something similar, but the feature should be internalized by the jQuer team because jQuery.modularize() would create awkward code like
$('div.myclass').myNs().doSomething();
... where we would of course like to see:
$('div.myclass').myNs.doSomething();
Actually, the test code John wrote was exactly what I wanted.
It would let me declare my namespace and methods as follows:
jQuery.space("myNs", function($){
$.fn.doSomething = function(){
return this.each(function(){
// code
});
};
});
The thing is that the jQuery selector initialization code has changed and broken Johns code.
Imagine that the methods jQuery.UI declared could all be grouped as follows:
$("#birds").ui.autocomplete({ ... })
$("pane.drag").ui.draggable({ ... })
$("pane.drop").ui.droppable({ ... })
etc...
Changed January 20, 2011 07:08PM UTC by comment:3
People have been dreaming of this syntax for some time, but when it comes down to creating an actually WORKING implementation that everyone can agree on and truly works gracefully with all the edge cases of chaining, going back and forth from namespaced methods and vice versa, it will probably continue to be just that: a dream.
While I and others see the value offered, every time, discussions of implementation quickly get kind of hairy. I'll grant that John's solution is neat, but is fraught with some caveats that you even outlined in a blogpost: http://malnotna.wordpress.com/2009/01/14/jqueryspace/
For one thing, the semantics of .end() get pretty weird - are you exiting a namespace or reverting the last traversal? Also, selecting elements to alter plugin defaults is really dodgy territory, if you ask me.
Since a pattern like this raises so many questions, I think most people would agree that until some sort of pattern emerges for doing this that is graceful and gains wide adoption, it shouldn't go into core...
Changed January 21, 2011 07:19AM UTC by comment:4
Thanks for the reply. Basically, I don't undestand the need for an end() statement to stop use a namespace at all:
Each method-evaluation should stand on it's own. For instance, in this chain:
$("#something").my.prepare().fadeIn(200).ui.draggable({...});
I see no reason as to why there should be a problem resolving in which namespace the prepare() or the draggable() methods reside, nor that fadeIn() is called in the default namespace...
If this is a problem in John's approach, Gilberto Saraiva's plugin comes closer to the goal. The one (admittedly showstopper) bug in his code is that you cannot cache a selector object:
var e = $("#foo"); $("#bar").fadeIn(200); e.my.collapse(); // will actually collapse #bar
For a software vendor with a range of jQuery plugins, what would you advise use to do if namespacing is not an option? Prefix all our methods?
$("#").ixs_foo().ixs_bar().ixs_ugly();
Changed February 02, 2011 12:17PM UTC by comment:5
resolution: | → patchwelcome |
---|---|
status: | new → closed |
I don't see the feature, in the requested form, making it into core anytime soon.
You may want to check out http://api.jquery.com/jQuery.sub which is included since 1.5.
If you happen to come up with a nice clean solution yourself please make a pull request.
Thanks for taking the time to contribute to the jQuery project by writing a feature request!
Could you please add in more information what you expect this to look like. Examples, use cases, ....
Also did you already check out the new subclass feature which landed in the 1.5b1 release?
Or are you more looking for something similar to jQuery.Modularize