Cleanup KnockoutJS

This commit is contained in:
djmaze 2021-09-28 14:40:24 +02:00
parent 4c7ce61bc0
commit 4142526ba6
9 changed files with 121 additions and 279 deletions

View file

@ -11,7 +11,7 @@ ko.bindingHandlers['attr'] = {
// To cover cases like "attr: { checked:someProp }", we want to remove the attribute entirely
// when someProp is a "no value"-like value (strictly null, false, or undefined)
// (because the absence of the "checked" attr is how to mark an element as not checked, etc.)
var toRemove = (attrValue === false) || (attrValue === null) || (attrValue === undefined);
var toRemove = (attrValue === false) || (attrValue == null);
if (toRemove) {
namespace ? element.removeAttributeNS(namespace, attrName) : element.removeAttribute(attrName);
} else {

View file

@ -4,7 +4,7 @@ ko.bindingHandlers['style'] = {
ko.utils.objectForEach(value, (styleName, styleValue) => {
styleValue = ko.utils.unwrapObservable(styleValue);
if (styleValue === null || styleValue === undefined || styleValue === false) {
if (styleValue == null || styleValue === false) {
// Empty string removes the value, whereas null/undefined have no effect
styleValue = "";
}

View file

@ -20,7 +20,7 @@ ko.bindingHandlers['textInput'] = {
var updateView = () => {
var modelValue = ko.utils.unwrapObservable(valueAccessor());
if (modelValue === null || modelValue === undefined) {
if (modelValue == null) {
modelValue = '';
}

View file

@ -60,7 +60,7 @@ ko.bindingHandlers['value'] = {
// For file input elements, can only write the empty string
updateFromModel = () => {
var newValue = ko.utils.unwrapObservable(valueAccessor());
if (newValue === null || newValue === undefined || newValue === "") {
if (newValue == null || newValue === "") {
element.value = "";
} else {
ko.dependencyDetection.ignore(valueUpdateHandler); // reset the model to match the element