Change KnockoutJS to just warn for missing Observables.

Sieve doesn't need them, for now.
This commit is contained in:
the-djmaze 2024-09-24 11:33:49 +02:00
parent 76ade5eac0
commit 96d034b36a
17 changed files with 78 additions and 130 deletions

View file

@ -1362,9 +1362,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);
@ -1383,9 +1383,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;
@ -1426,34 +1427,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) =>
@ -1471,8 +1451,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);
}