Usually there is some logic you want to happen in the browser, without a server round-trip. Some examples could be hiding some content or changing the validation of a field according to a field value.
Instead of making you write frontend code, Mateu provides kind of business rules support for changing properties of things on the browser according to the component state (or what is the same, the field values). This approach is indeed a good fit for 99.9999% of the use cases I have found.
So, you declare a list of rules like below:
Those rules are sequentially evaluated and, for each rule, if the filter evaluation return truthy then the rule is applied. This way you can change attributes in the browser without a server round-trip.
More or less, the logic is this: the sentence “field x is hidden if y == 5 and z” is always true, and Mateu will make sure it is always true. For stating this logic, we would create a rule like this:
| Filter | Action | Field name | Field attribute | Value | Expression | Result |
|---|---|---|---|---|---|---|
| true | UpdateData | x | hidden | state.y == 5 && state.z | Continue |
The rule above would be defined, like always, from our java class:
- Declarative
- Imperatively
Just annotate your class with @Rule, as in the example below:
@Rule(
filter = "true",
action = RuleAction.UpdateData,
fieldName = "x",
fieldAttribute = RuleFieldAttribute.hidden,
expression = "state.y == 5 && state.z",
result = RuleResult.Continue
)
public class VisibilityRulePage {
}
If you want to declare your rule using a fluent style code you can do so by implementing the RuleSupplier interface, like in the example below:
public class VisibilityRulePage implements ComponentTreeSupplier, RuleSupplier {
// ...
@Override
public List<Rule> rules() {
return List.of(
Rule.builder()
.filter("true")
.action(RuleAction.UpdateData)
.fieldName("x")
.fieldAttribute(RuleFieldAttribute.hidden)
.expression("state.y == 5 && state.z")
.result(RuleResult.Continue)
.build()
);
}
}
Please notice there are some fields (filter, result) in the rule which could be avoided right now, but I have intentionally left them there as I expect this to have more functionality and need them in the near future.