How-to: Custom view controller API
This article collects typical tasks a custom view solves through its controller. The controller is specified in the Language articles: the members of a React view's controller in React view controller, and the form's controller, which a custom view of any kind gets, and the navigator's controller in Form and navigator controllers.
An end-to-end example of these calls from a classic custom view is in How-to: Custom Components (server calls).
Changing the current object and a value from a component
We need a click on a row to make the row current, and a button to mark the current order. The solution uses the group's change(row) and the change(value) of the property's member (Changes to the current object and property values).
function orderView(props) {
const controller = props.controller;
return (
<div>
<button onClick={() => controller.o.note.change('checked')}>Mark</button>
{props.data.o.list.map(row =>
<div key={row.key} onClick={() => controller.o.change(row)}>
{row.number.value}
</div>)}
</div>
);
}
Suggestions as the user types
We need to show suggestions for a value the user types and to make a suggestion the group's current object. The solution uses getValues (Value lookup).
A view can show the first answer at once, the final one when it comes, and the message of a rejection:
try {
let result = await controller.i.name.getValues(query);
show(result.data, result.more != null);
if (result.more) {
result = await result.more;
show(result.data, false);
}
} catch (e) {
if (!e.cancelled)
showHint(e.message); // e.g. a query too short to search
}
Listing the suggestions and picking the first one:
const result = await controller.c.customer.getValues(text);
result.data.forEach(item => console.log(item.displayString));
// picking the first suggestion of the final answer as the group's customer
const item = (result.more ? await result.more : result).data[0];
if (item) controller.c.change(item.objects);
Declaring editing for a field the view draws
We need a note field the view draws itself, in which ENTER does not move the focus to the next component and ESCAPE does not close the window. The solution uses the editing declaration on the form's controller.
function Note({ controller }) {
return <textarea onFocus={e => controller.form.startEditing(e.target)}
onBlur={e => controller.form.stopEditing(e.target)}/>;
}
This example declares on focus.