Skip to content

Commit

Permalink
Fixed issue tuupola#11 by implementing suggested code
Browse files Browse the repository at this point in the history
Coverted variables describing what's currently selected into an array.
This breaks the tests, as it appears not to be compatible with the
support for multiple parents to a selector.
  • Loading branch information
benwbrum committed Nov 12, 2013
1 parent ec5582c commit d48884a
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions jquery.chained.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@
$(self).html(backup.html());

/* If multiple parents build classname like foo\bar. */
var selected = "";
var selected = [];
$(parent_selector).each(function() {
if ($(":selected", this).val()) {
selected += "\\" + $(":selected", this).val();
}
$(":selected", this).each(function() {
selected.push($(this).val());
});
});
selected = selected.substr(1);

/* Zepto class regexp dies with classes like foo\bar. */
if (window.Zepto) {
selected = selected.replace("\\", "\\\\");
}
/* Also check for first parent without subclassing. */
/* TODO: This should be dynamic and check for each parent */
/* without subclassing. */
Expand All @@ -59,11 +54,21 @@
var selected_first = $(":selected", first).val();

$("option", self).each(function() {
/* Remove unneeded items but save the default value. */
if (!$(this).hasClass(selected) &&
!$(this).hasClass(selected_first) && $(this).val() !== "") {
$(this).remove();
}


var hasSelectedClass = false;
var classList = ($(this).attr('class') == undefined || "" ) ? [] : $(this).attr('class').split(/\s+/);
$.each( classList, function(index, item){
if (jQuery.inArray(item, selected) >= 0) {
hasSelectedClass = true;
return;
}
});
if (!hasSelectedClass &&
!$(this).hasClass(selected_first) && $(this).val() !== "") {
$(this).remove();
}

});

/* If we have only the default value disable select. */
Expand Down

0 comments on commit d48884a

Please sign in to comment.