Skip to content

Computed Properties

This plugin allows users to specify a path in the data-model (binding) as a computed property in the schema. Anytime this binding is read from, the given expression will be evaluated and returned instead of the it being read from the actual model. Writes to the binding will be prevented, and an error will be thrown.

Usage

Add the plugin to Player:

import { Player } from "@player-ui/player";
import { ComputedPropertiesPlugin } from "@player-ui/computed-properties-plugin";
const player = new Player({
plugins: [new ComputedPropertiesPlugin()],
});

Expression Data Type

The computed properties plugin introspects the schema, looking for any DataType that uses the Expression:

{
"type": "Expression",
"exp": "@[ someExpression() ]@"
}

Any data-lookup for that binding path will evaluate the given expression and return that value. Results are not cached, and will be recomputed on every fetch.

Example

{
"schema": {
"ROOT": {
"foo": {
"type:" "FooType"
}
},
"FooType": {
"computedValue": {
"type": "Expression",
"exp": "1 + 2 + 3"
}
}
}
}

Using the above schema, any reference to {{foo.computedValue}} will compute the 1 + 2 + 3 expression and use that as the underlying value for that path.

Any write or set operation to {{foo.computedValue}} will result in a thrown exception for writing to a read-only path.