DataProxy
import { DataProxy } from 'cx/ui';The simplest use case for DataProxy is when we want to create an alias for a certain store binding.
In the example below, level binding is also made available as $level.
This creates a simple two-way mapping between the two store values. Moving one slider will affect the other.
Defining multiple aliases
data property is used to define multiple mappings. data is an object whose property names serve as aliases,
and their values are objects with expr and set properties that define custom getter and setter logic:
exprdefines a getter logic and can be a Cx computable or an expression,setis a function that receives the alias value and theinstanceobject as parameters. Thestorecan be accessed directly with destructuring assignment syntax. Note that the setter function needs to call thestore.setmethod explicitly in order to set thelevelvalue, as opposed to just returning the calculated value. This is because we can use any number of store values to calculate the alias, and it's up to us to define the setter logic correctly.
Omitting the set property will make the alias itself a read-only. Attempting to change its value will log
an error to the console, so the UI should not allow it.
<FlexRow spacing="xlarge" align="start">
<LabelsTopLayout>
<Slider value-bind="level" label="Level" />
</LabelsTopLayout>
<DataProxy
data={{
$invertedLevel: {
expr: computable("level", (v) => 100 - v),
set: (value, { store }) => {
store.set("level", 100 - value);
}
},
$level: {
expr: "{level}"
}
}}
>
<FlexCol>
<Slider value-bind="$invertedLevel" label="Inverted level" />
<Slider value-bind="$level" label="Level (read-only)" />
</FlexCol>
</DataProxy>
</FlexRow>If mapping is done in both directions (both getter and setter are used), it is important that both operations are reversible, without any data loss. This means, for any alias value, we should be able to get back all of the store values that were used to calculate it. Failing to do so will cause bugs that are hard to detect.
Note: It is good practice to prefix the alias name with a $ sign in order to avoid unintentional name shadowing
which will cause an infinite get-set loop and a Maximum call stack exceeded error.
Advanced example
Exposing currently selected Grid record for real time editing is a common use case for DataProxy.
Configuration
| Property | Description | Type |
|---|---|---|
alias | Alias for computed value. | string |
data | Object whose property names serve as aliases, and their values are objects with | object |
value | Cx binding, computable, expression or an object with | object |
immutable | Indicate that the data in the parent store should not be mutated. Defaults to | boolean |
items | List of child elements. | array |
layout | Define an inner layout. | string/object |
mod | Appearance modifier. For example, | string/array |
outerLayout | Defines the outer layout which wraps the widget. | widget |
plainText | Set to | boolean |
preserveWhitespace | Keep whitespace in text based children. Default is | boolean |
putInto | Used with outer layouts. Specifies the name of the content placeholder which should render the widget. | string |
trimWhitespace | Remove all whitespace in text based children. Default is | boolean |
vdomKey | Key that will be used as the key when rendering the React component. | string |
visible | Visibility of the widget. Defaults to | boolean |