KnockoutJS don't throw error when property is not observable

This commit is contained in:
djmaze 2024-09-24 11:45:17 +02:00
parent 3893849d21
commit 1b0dd48cdb
3 changed files with 65 additions and 104 deletions

View file

@ -45,9 +45,9 @@ ko.expressionRewriting = (() => {
divisionLookBehind = /[\])"'A-Za-z0-9_$]+$/,
keywordRegexLookBehind = {'in':1,'return':1,'typeof':1},
parseObjectLiteral = objectLiteralString => {
preProcessBindings = bindingsStringOrKeyValueArray => {
// Trim leading and trailing spaces from the string
var str = ko.utils.stringTrim(objectLiteralString);
var str = ko.utils.stringTrim(bindingsStringOrKeyValueArray);
// Trim braces '{' surrounding the whole object literal
if (str.charCodeAt(0) === 123) str = str.slice(1, -1);
@ -66,9 +66,10 @@ ko.expressionRewriting = (() => {
// A comma signals the end of a key/value pair if depth is zero
if (c === 44) { // ","
if (depth <= 0) {
result.push((key && values.length)
? {key: key, value: values.join('')}
: {'unknown': key || values.join('')});
if (key && values.length) {
// Values are wrapped in a function so that each value can be accessed independently
result.push("'" + key + "':()=>(" + values.join('') + ")");
}
key = depth = 0;
values = [];
continue;
@ -109,34 +110,13 @@ ko.expressionRewriting = (() => {
throw Error("Unbalanced parentheses, braces, or brackets");
}
}
return result;
},
preProcessBindings = (bindingsStringOrKeyValueArray) => {
result.push("'$data':()=>$data");
var resultStrings = [],
// propertyAccessorResultStrings = [],
keyValueArray = parseObjectLiteral(bindingsStringOrKeyValueArray),
processKeyValue = (key, val) => {
// Values are wrapped in a function so that each value can be accessed independently
val = 'function(){return ' + val + ' }';
resultStrings.push("'" + key + "':" + val);
};
keyValueArray.forEach(keyValue =>
processKeyValue(keyValue.key || keyValue['unknown'], keyValue.value)
);
/*
if (propertyAccessorResultStrings.length)
processKeyValue('_ko_property_writers', "{" + propertyAccessorResultStrings.join(",") + " }");
*/
return resultStrings.join(",");
return result.join(",");
};
return {
parseObjectLiteral: parseObjectLiteral,
preProcessBindings: preProcessBindings,
keyValueArrayContainsKey: (keyValueArray, key) =>
@ -154,8 +134,9 @@ ko.expressionRewriting = (() => {
// it is !== existing value on that writable observable
writeValueToProperty: (element, property, allBindings, key, value, checkIfDifferent) => {
if (!property || !ko.isObservable(property)) {
throw Error(`"${key}" must be observable for ${element.outerHTML.replace(/>.+/,'>')}`);
// allBindings.get('_ko_property_writers')?.[key]?.(value);
console.error(`"${key}" should be observable in ${element.outerHTML.replace(/>.+/,'>')}`);
// ko.dataFor(element).key = value;
allBindings.get('$data')[key] = value;
} else if (ko.isWriteableObservable(property) && (!checkIfDifferent || property.peek() !== value)) {
property(value);
}