Drop support for _ko_property_writers and for two-way binding they must be observables

This commit is contained in:
the-djmaze 2024-01-08 05:25:48 +01:00
parent 81ccee1152
commit e55938b653
3 changed files with 84 additions and 76 deletions

View file

@ -1,5 +1,7 @@
ko.expressionRewriting = (() => {
var javaScriptReservedWords = ["true", "false", "null", "undefined"],
var
/*
javaScriptReservedWords = ["true", "false", "null", "undefined"],
// Matches something that can be assigned to--either an isolated identifier or something ending with a property accessor
// This is designed to be simple and avoid false negatives, but could produce false positives (e.g., a+b.c).
@ -12,7 +14,7 @@ ko.expressionRewriting = (() => {
var match = expression.match(javaScriptAssignmentTarget);
return match === null ? false : match[1] ? ('Object(' + match[1] + ')' + match[2]) : expression;
},
*/
// The following regular expressions will be used to split an object-literal string into tokens
specials = ',"\'`{}()/:[\\]', // These characters have special meaning to the parser and must not appear in the middle of a token, except as part of a string.
@ -116,19 +118,20 @@ ko.expressionRewriting = (() => {
preProcessBindings = (bindingsStringOrKeyValueArray) => {
var resultStrings = [],
propertyAccessorResultStrings = [],
// propertyAccessorResultStrings = [],
keyValueArray = parseObjectLiteral(bindingsStringOrKeyValueArray),
processKeyValue = (key, val) => {
var writableVal, obj = ko.bindingHandlers[key];
var /*writableVal,*/ obj = ko.bindingHandlers[key];
if (obj?.['preprocess'] && !obj['preprocess'](val, key, processKeyValue))
return;
/*
if (twoWayBindings.has(key) && (writableVal = getWriteableValue(val))) {
// For two-way bindings, provide a write method in case the value
// isn't a writable observable.
propertyAccessorResultStrings.push("'" + key + "':function(_z){" + writableVal + "=_z}");
}
*/
// Values are wrapped in a function so that each value can be accessed independently
val = 'function(){return ' + val + ' }';
resultStrings.push("'" + key + "':" + val);
@ -137,10 +140,10 @@ ko.expressionRewriting = (() => {
keyValueArray.forEach(keyValue =>
processKeyValue(keyValue.key || keyValue['unknown'], keyValue.value)
);
/*
if (propertyAccessorResultStrings.length)
processKeyValue('_ko_property_writers', "{" + propertyAccessorResultStrings.join(",") + " }");
*/
return resultStrings.join(",");
};
@ -167,7 +170,8 @@ ko.expressionRewriting = (() => {
// it is !== existing value on that writable observable
writeValueToProperty: (property, allBindings, key, value, checkIfDifferent) => {
if (!property || !ko.isObservable(property)) {
allBindings.get('_ko_property_writers')?.[key]?.(value);
throw Error(`${key} , must be observable`);
// allBindings.get('_ko_property_writers')?.[key]?.(value);
} else if (ko.isWriteableObservable(property) && (!checkIfDifferent || property.peek() !== value)) {
property(value);
}