Skip to content

k.state

View data (ViewData) in the current request rendering context

Overview

k.state reads and writes key values ​​in the view data stack of the current HTTP request for use by page, layout, and view template binding (for example, k-state-key or reading data with the same name in the template). The data is released when the request ends and is not persisted across requests.

::: The difference between tip, k.cache and k.session

APIScopeTypical uses
k.stateView rendering within a single requestController/CodeBlock passes product, breadcrumb to the template
k.cacheSite-level memory, expirableCalculation results shared across requests
k.sessionguest sessionLogin status, shopping cart temporary data
:::

Must be used in the process involved in HTML page rendering (page CodeBlock, layout script, etc.). In requests where pure k.api returns JSON and does not render templates, set/get are usually only visible to the current script and will not appear in the response HTML.

TypeScript Definition

ts
interface KState {
  set(key: string, value: any): void;
  setCurrent(key: string, value: any): void;
  get(key: string): any;
}

set()

Push a piece of named data to the view data stack (which can form a hierarchy with subsequent set and be parsed by the rendering engine).

ts
const product = { id: "1", name: "Sample" }
k.state.set("product", product)
ParameterTypeRequiredDescription
keystringyesKey names used in templates
valueanyyesany serializable object or value

Returns: void.

setCurrent()

Writes or overwrites the key on the top frame of the current stack; if the stack is empty, the behavior is the same as set.

ts
k.state.set("items", list)
k.state.setCurrent("items", updatedList)
ParameterTypeRequiredDescription
keystringyesKey name
valueanyyesnew value

Returns: void.

get()

Press the key to read the view data (parsed from the current data context, including the outer stack).

ts
const product = k.state.get("product")
ParameterTypeRequiredDescription
keystringyesKey name

Returns: The value previously written to set / setCurrent; undefined or empty if not present (whichever is runtime).

Example: List page passes data to template

ts
// Page CodeBlock
const items = k.DB.sqlite.table("Product").all()
k.state.set("products", items)
k.page.setTitle("Product List")
ts
// Read from another script
const products = k.state.get("products")