Skip to content

feat(8.2-alpha): Addition of "global" context for expressions inside XML #9734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions packages/core/ui/core/bindable/bindable-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,23 @@ const expressionParsers = {
object = getContext(expression.callee.name, model, changedModel);
property = expression.callee?.name;
}

const callback = expression.callee.optional ? object?.[property] : object[property];
if (isNullOrUndefined(callback)) {
throw new Error('Cannot perform a call using a null or undefined property');
}

const isConverter = isObject(callback) && (isFunction(callback.toModel) || isFunction(callback.toView));
if (!isFunction(callback) && !isConverter) {
throw new Error('Cannot perform a call using a non-callable property');
}

const parsedArgs = [];
for (let argument of expression.arguments) {
let value = convertExpressionToValue(argument, model, isBackConvert, changedModel);
argument.type == 'SpreadElement' ? parsedArgs.push(...value) : parsedArgs.push(value);
}

if (isNullOrUndefined(callback) || (!isFunction(callback) && !isConverter)) {
throw new Error('Cannot perform a call using a non-function property');
}

return isConverter ? getConverter(callback, parsedArgs, isBackConvert) : callback.apply(object, parsedArgs);
return isConverter ? getConverter(callback, object, parsedArgs, isBackConvert) : callback.apply(object, parsedArgs);
},
'ChainExpression': (expression: ASTExpression, model, isBackConvert: boolean, changedModel) => {
return convertExpressionToValue(expression.expression, model, isBackConvert, changedModel);
Expand Down Expand Up @@ -174,12 +177,16 @@ const expressionParsers = {
};

function getContext(key, model, changedModel) {
return key in changedModel ? changedModel : model;
let context = key in changedModel ? changedModel : model;
if (!(key in context)) {
context = global;
}
return context;
}

function getConverter(context, args, isBackConvert: boolean) {
function getConverter(converterSchema, context, args, isBackConvert: boolean) {
const converter = { callback: null, context, args };
let callback = isBackConvert ? context.toModel : context.toView;
let callback = isBackConvert ? converterSchema.toModel : converterSchema.toView;
if (callback == null) {
callback = Function.prototype;
}
Expand Down