KScript docs
EN

Web pages

Model forms

kscript make component may write a component for an existing model, or a model and its component together:

CommandWhat it does
kscript make component NoteCard --model Note --fields title,bodyA component that shows a Note.
kscript make model Note --fields "title: string, body: string" --componentA model, and a component for it.

The first command writes _components/note-card.kscript. The component takes the row as an input named after its model, so a page renders it with <note-card note="{$note}" />:

<import package="KScript.Web" />
<import path="../models/note.kscript" />

<type name="NoteCard" extends="Component">
  <construct>
    <field name="note" type="Note" />
  </construct>

  <fn name="render" value="```html
      &lt;section class=&quot;note-card&quot;>
        &lt;p>{$note.title}&lt;/p>
        &lt;p>{$note.body}&lt;/p>
        &lt;slot />
      &lt;/section>
      ```" />
</type>
import KScript.Web
import "../models/note.kscript"

type NoteCard extends Component {
  construct({ note: Note })

  fn render() => ```html
    <section class="note-card">
      <p>{$note.title}</p>
      <p>{$note.body}</p>
      <slot />
    </section>
    ```
}

The tag's attributes fill the component's inputs, which render() reads as $note. Computed fields, local constants, and methods called with $this->method() are there for the HTML too. The editor completes props on the tag, and model fields inside the fence. A Component needs fn render().

Add --inject to take the record from the route instead, so the page writes <note-card /> on /notes/{note}. The component asks the model's policy as it takes the record: construct({ note: Note = @inject(can: read) }). Give another ability with --inject update. See Take the route's record with @inject().

Saving edits to a model

A component may save edits straight to a KScript.Data model. Declare the fields that may be written, and who owns the row, once in the web routes. Here a Profile model holds a user_id and a bio:

<KScript.Routes>
  <ModelUpdate name="edit_profile" model="Profile" owner="user_id" fields="bio" />
</KScript.Routes>
import KScript.Http

Routes {
  ModelUpdate(name: edit_profile, model: Profile, owner: user_id, fields: [bio])
}

Then generate the editor:

CommandWhat it does
kscript make component ProfileEditor --model-update edit_profileA form that saves the bio.

Pass the owned row as <profile-editor profile="{$profile}" />. A protected GET controller loads the signed-in user's row and passes it to the page, which renders <profile-editor profile="{$data.profile}" />. The auth sample includes this profile editor and its migration. For a component with its own interactive state, start with kscript make component OrderDetails --live.

The generated form saves when Save is clicked. Typing leaves the database alone until then. In a live block, Save updates the model in place and a message confirms it. A form outside a live block posts as usual and redirects back, so it works with JavaScript turned off.

Add --autosave to save when typing pauses, after 500 ms, or choose a delay with autosave="750ms". Save still sends pending edits at once. The runtime sends only edited fields, waits for each save to finish, and keeps the draft when validation fails.

How saves are checked

KScript writes the POST handler and includes a CSRF token. Every save checks the session and token, matches the row's user_id with the signed-in user's id, and validates the fields against the model and the binding's list of fields. Use owner: id when the row's key is the account id, and add route middleware to ModelUpdate for permissions of your own. Check ownership when reading the row too. Keep SQLite files of your own outside the public site, or in a folder whose name starts with _.