update $(this) from within a plugin call

update $(this) from within a plugin call


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.