Owned model list
Declare tasks: children.many(Task) in Board, create the board from data and pass it to view. For receives board.tasks and supplies each TaskRow's ready model.
The maintained example uses For each/view. Row state/actions are declared in the model, not view.
Insert creates a row; move transfers existing DOM/ref attachments; remove awaits lifecycle and destroys that child. Disposing Board cascades through children. Static arrays render once; reactive identity/operations belong to the source, not JSX.
See For types and list mechanics.
Complete example sources
ts
import { computed, reaction } from "@virentia/core";
import { children, f, staticModel, type InstanceOf } from "@virentia/core/models";
import { action, ref, ui } from "@virentia/ui";
export const Task = staticModel({
name: "CourseTask", with: [ui],
data: { title: f.string(), draft: f.string(""), done: f.boolean(false) },
setup(self) {
const input = ref<HTMLInputElement>();
const changeDraft = action((event: InputEvent) => {
self.draft.value = (event.currentTarget as HTMLInputElement).value;
});
const save = action(() => { const text = self.draft.value.trim(); if (text) self.title.value = text; });
const toggleDone = action(() => { self.done.value = !self.done.value; });
reaction({ on: self.mounted, run: () => input.value?.focus() });
return { input, changeDraft, save, toggleDone, canSave: computed(() => !!self.draft.value.trim()) };
},
});
export const Board = staticModel({
name: "CourseBoard",
data: { title: f.string("Virentia Tasks"), editorOpen: f.boolean(false), newTitle: f.string(""), tasks: children.many(Task) },
setup(self) {
const toggleEditor = action(() => { self.editorOpen.value = !self.editorOpen.value; });
const changeNewTitle = action((event: InputEvent) => {
self.newTitle.value = (event.currentTarget as HTMLInputElement).value;
});
const addTask = action((event: SubmitEvent) => {
event.preventDefault();
const title = self.newTitle.value.trim();
if (title) self.tasks.add({ title, draft: title });
self.newTitle.value = "";
});
return { toggleEditor, changeNewTitle, addTask, count: computed(() => self.tasks.count) };
},
});
export type TaskInstance = InstanceOf<typeof Task>;
export type BoardInstance = InstanceOf<typeof Board>;
export type TaskData = { id: string; title: string; done?: boolean };
export const seedTasks: TaskData[] = [{ id: "task-1", title: "Learn Virentia" }];
export function createBoard(tasks: readonly TaskData[] = seedTasks, id = "board", title = "Virentia Tasks"): BoardInstance {
return Board.create({ id, title, tasks: tasks.map((task) => ({ ...task, draft: task.title })) });
}tsx
/** @jsxImportSource @virentia/ui */
import { component, For } from "@virentia/ui";
import { Board, Task } from "./models";
export const TaskEditor = component({ model: Task, view: (_props, self) =>
<section><label>Draft <input ref={self.model.input} value={self.model.draft} onInput={self.model.changeDraft} /></label>
<button onClick={self.model.save}>Save</button></section>,
});
export const TaskRow = component({ model: Task, view: (_props, self) =>
<li><input type="checkbox" checked={self.model.done} onChange={self.model.toggleDone} /><strong>{self.model.title}</strong></li>,
});
export default component({ model: Board, view(_props, self) {
function content() {
const task = self.model.tasks.items[0];
if (self.model.editorOpen.value && task) return <TaskEditor model={task} />;
return <ul><For each={self.model.tasks} view={TaskRow} fallback={<li>No tasks</li>} /></ul>;
}
return <main><h1>{self.model.title}</h1>
<form onSubmit={self.model.addTask}>
<input value={self.model.newTitle} onInput={self.model.changeNewTitle} /><button>Add</button>
</form>
<button onClick={self.model.toggleEditor}>Switch list/editor</button>
<p>Tasks: {self.model.count}</p>{content}
</main>;
} });tsx
/** @jsxImportSource @virentia/ui */
import { component, For, render, scope, scoped } from "@virentia/ui";
import { Board, createBoard } from "./tasks/models";
import { TaskRow } from "./tasks/App";
const BoardView = component({ model: Board, view: (_props, self) => <ul><For each={self.model.tasks} view={TaskRow} /></ul> });
export function mountTasks(container: Element) {
const appScope = scope();
const board = scoped(appScope, () => createBoard());
const unmount = render(() => <BoardView model={board} />, container, { scope: appScope });
return { board, unmount };
}