Thanks for replying.
Correct me if I'm wrong but what you're suggesting is manipulating the
set only in memory and returning a set of unique elements in a jQuery
object. I would have to add it back to the DOM afterwards (removing
the original set before from the DOM) ?

I have looked at the jQ code of pushStack and traced it but sorry I
don't get it. There is not much example on the web. My code was taken
from here : http://dev.jquery.com/ticket/255.

Still, I tryed to refactor my function for something more in the
spirit of jQ. There is no DOM manipulation. It's faster but I wonder
what you would have done, could you give me a lead ?

          jQuery.fn.unique = function() {
            function internalEqual(a,b) {
            // this is for making change easily to the comparison
function
              var v1,v2;
              v1 = a.innerText || a.textContent;
              v2 = b.innerText || b.textContent;
              return (v1 === v2);
            }

            var that, arraySorted, arraySortedUnique;
            that = $(this).sort();
            arraySorted = that.get();
            arraySortedUnique = jQuery.grep(arraySorted, function
(elem, i) {
                      if (i < arraySorted.length-1) {
                        return (!internalEqual(elem, arraySorted[i
+1]));
                        }
                      return true;
                    });
            return jQuery(arraySortedUnique);
            }; // close unique

Thank you.

On 18 août, 08:40, John Resig <jere...@gmail.com> wrote:
> Well, you could just manipulatethis[0],this[1], etc. directly - but it's
> strongly preferred that you return the new result withpushStack(as you've
> done) since that conforms with the typical way of constructing jQuery
> methods and plugins.
>
> --John
>
> On Mon, Aug 17, 2009 at 3:46 PM, jeanph01 <jeanp...@gmail.com> wrote:
>
> > Hi!
>
> > I'm creating a jquery plugin that alter an object to remove duplicates
> > in it. My problem is that a variable that access the old selector
> > result does not contain the same elements that it would have contained
> > if it would have been executed after the duplicates removal.
> > To illustrate :
>
> > var v = $('div);   // 150 objects
> > v.removeDuplicate();   // remove 10 duplicates
> > // here v still point to 150 objects
>
> > Here is my plugin :
>
> > jQuery.fn.__sort = function() {
> >    return this.pushStack([].sort.apply(this, arguments), []);
> > };
>
> > jQuery.fn.sort = function(func) {
> >    function internalSort(a,b){
> >        var compA = $(a).text();
> >        var compB = $(b).text();
> >        return (compA <> compB) ? 1 : 0;
> >    }
> >    if (!$.isFunction(func)) {
> >        func = internalSort;
> >    }
> >    return this.__sort(func);
> > };
>
> > jQuery.fn.removeDuplicate = function() {
> >    var that = $(this).sort();
>
> >    $(this).each(function() {
> >        var pos = that.index($(this));
> >        var next = that.eq(pos+1);
> >        var eq = (next.text() === $(this).text());
>
> >        if (eq) { next.remove();}
> >    });
>
> >    // enable chaining, updating selector
> >    return $($(this).selector);
> > };
>
> > The only solution I found is to return $(this) with an updated
> > selector. But I would have had to remember to update the content of my
> > variable before calling the plugin.
> > ex:
> > v = v.removeDuplicates();
>
> > Can I update $(this) from within removeDuplicate() ??
>
> > Thank you
>
> > note: I posted this on the jQuery Plugins discussion put it seems
> > dead, or full of spam.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to