Module ValueHosts/Types/CalcValueHost

CalcValueHost is a specialized ValueHost whose value is calculated when its getValue method is called. You supply a function callback in its CalcValueHostConfig to set it up.

Calculations allow you to expand what is available to Conditions without having to create new rules. This class was inspired by this use case:

The user wants to compare two dates to determine if the number of days between them is greater to (or equal or less than) another value. With CalcValueHost, the user can use the GreaterThanCondition to compare two integers, one being the value returned by a CalcValueHost and the other is the number of days to compare.

CalcValueHost has nifty conversion functions built in, that can be used to prepare the values it needs, the same way the comparison conditions do. let totalDays = vh.convert(value, 'TotalDays'); // 'TotalDays' is a lookup key Internally CalcValueHost uses Jivs' DataTypeConverters and DataTypeIdentifiers to convert the original value into the value demanded by its own dataType property.

Here is pseudo code for configuring the CalcValueHost used in this example.

function differenceBetweenDates(callingValueHost: ICalcValueHost, findValueHosts: IValueHostsManager)
: SimpleValueType
{
let totalDays1 = callingValueHost.convert(findValueHosts.getValueHost('StartDate')?.getValue(), LookupKey.TotalDays);
let totalDays2 = callingValueHost.convert(findValueHosts.getValueHost('EndDate')?.getValue(), LookupKey.TotalDays);
if (typeof totalDays1 !== 'number' || typeof totalDays2 !== 'number')
return undefined; // can log with findValueHosts.services.logger.log();
return Math.abs(totalDays2 - totalDays1);
}

// create the CalcValueHostConfig to supply to the ValidationManager
let builder = build(services);
builder.calc('DiffDays', LookupKey.Integer, differenceBetweenDates);

// create the 'StartDate' input with a LessThanCondition
builder.input('StartDate', 'Date', { label: 'Start date' })
.lessThan(10, { valueHostName: 'DiffDays' });

Your function can also save stateful information with the valueHost.saveIntoInstanceState.

Index

Interfaces

Type Aliases

Generated using TypeDoc v0.25.12