Bug Tracker

Modify

Ticket #8020 (closed feature: patchwelcome)

Opened 2 years ago

Last modified 2 years ago

Native namespace support in selector methods

Reported by: marc@… Owned by:
Priority: low Milestone: 1.next
Component: core Version: 1.5b1
Keywords: Cc:
Blocking: Blocked by:

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.

Change History

comment:1 Changed 2 years ago by jitter

  • Priority changed from undecided to low
  • Component changed from unfiled to core

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

comment:2 Changed 2 years ago by marc@…

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...

comment:3 Changed 2 years ago by ajpiano

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...

comment:4 Changed 2 years ago by marc@…

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();

comment:5 Changed 2 years ago by jitter

  • Status changed from new to closed
  • Resolution set to patchwelcome

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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.