COMMUNITY

Best way to trigger the rendering of a child view

In a Coconut application, I have a child view that displays a list of items fetched from a remote service.
The application has a header bar containing a button specifically designed to trigger a refresh of the item list. What is the best way to implement this behavior?

In vanilla JS, I would probably trigger an event on the window object and make the child view listen to these events and react accordingly. But I have strictly no idea of what is the Coconut equivalent (I’m very new to Coconut… and have no experience with React: I’m an Angular guy :smile: ).

Hard to say without more context, but well, in essence the easiest way to trigger reload/recomputation (and thus rerender) is to have an observable counter that the computation depends upon and bump it to trigger.

Example in a model:

class TodoListModel implements Model {
  @:observable var revision:Int = 0;
  @:constant var service:Void->Promise<List<TodoItem>>;
  @:loaded var items:List<TodoItem> = { 
    revision;// this makes `items` depend on `revision`
    service();
  }
  @:transition function reload() {
    return { revision: revision + 1 };// therefore, this triggers a reload of `items`
  }
}