KnockoutJS drop unused optionsCaption

This commit is contained in:
the-djmaze 2024-03-03 18:01:02 +01:00
parent bb3fc79e09
commit acf1be454f

View file

@ -62,15 +62,6 @@ ko.bindingHandlers['options'] = {
// Filter out any entries marked as destroyed // Filter out any entries marked as destroyed
filteredArray = unwrappedArray.filter(item => item || item == null); filteredArray = unwrappedArray.filter(item => item || item == null);
// If caption is included, add it to the array
if (allBindings['has']('optionsCaption')) {
captionValue = ko.utils.unwrapObservable(allBindings.get('optionsCaption'));
// If caption value is null or undefined, don't show a caption
if (captionValue != null) {
filteredArray.unshift(captionPlaceholder);
}
}
} else { } else {
// If a falsy value is provided (e.g. null), we'll simply empty the select element // If a falsy value is provided (e.g. null), we'll simply empty the select element
} }
@ -87,7 +78,7 @@ ko.bindingHandlers['options'] = {
} }
var option = element.ownerDocument.createElement("option"); var option = element.ownerDocument.createElement("option");
if (arrayEntry === captionPlaceholder) { if (arrayEntry === captionPlaceholder) {
ko.utils.setTextContent(option, allBindings.get('optionsCaption')); ko.utils.setTextContent(option);
ko.selectExtensions.writeValue(option, undefined); ko.selectExtensions.writeValue(option, undefined);
} else { } else {
// Apply a value to the option element // Apply a value to the option element