## Modules

* [Component](#module_component) ⇐ <code>View</code>
    * _instance_
        * [.createProps(props)](#module_component__createprops) ⇒ <code>Model</code>
        * [.provide()](#module_component__provide) ⇒ <code>object</code> \| <code>null</code>
        * [.inject(key)](#module_component__inject) ⇒ <code>Model</code>
        * [.subscribe(model, [type], [listener])](#module_component__subscribe) ⇒ <code>Component</code>
        * [.partial(strings, ...expressions)](#module_component__partial) ⇒ [<code>ComponentPartial</code>](#componentpartial)
        * [.template()](#module_component__template) ⇒ [<code>ComponentPartial</code>](#componentpartial) \| <code>Component</code>
        * [.toString()](#module_component__tostring) ⇒ <code>string</code>
        * [.render()](#module_component__render) ⇒ <code>Component</code>
        * [.onCreate(...args)](#module_component__oncreate)
        * [.onChange(model, changed)](#module_component__onchange)
        * [.onHydrate()](#module_component__onhydrate)
        * [.onBeforeRecycle()](#module_component__onbeforerecycle)
        * [.onRecycle()](#module_component__onrecycle)
        * [.onBeforeUpdate()](#module_component__onbeforeupdate)
        * [.onUpdate()](#module_component__onupdate)
        * [.onDestroy(...args)](#module_component__ondestroy)
    * _static_
        * [.markAsSafeHTML(value)](#module_component_markassafehtml) ⇒ <code>object</code>
        * [.extend(object)](#module_component_extend)
        * [.mount([options], [containerEl], [shouldHydrateExistingDOM])](#module_component_mount) ⇒ <code>Component</code>
        * [.create(strings, ...expressions)](#module_component_create) ⇒ <code>Component</code>
* [Emitter](#module_emitter)
    * [.on(type, listener)](#module_emitter__on) ⇒ <code>function</code>
    * [.once(type, listener)](#module_emitter__once) ⇒ <code>function</code>
    * [.off([type], [listener])](#module_emitter__off)
    * [.emit(type, [...args])](#module_emitter__emit)
    * [.listenTo(emitter, type, listener)](#module_emitter__listento) ⇒ <code>function</code>
    * [.listenToOnce(emitter, type, listener)](#module_emitter__listentoonce) ⇒ <code>function</code>
    * [.stopListening([emitter], [type], [listener])](#module_emitter__stoplistening)
* [Model](#module_model) ⇐ <code>Emitter</code>
    * _instance_
        * [.preinitialize([attributes], [...args])](#module_model__preinitialize)
        * [.defineAttribute(key)](#module_model__defineattribute)
        * [.get(key)](#module_model__get) ⇒ <code>any</code>
        * [.set(key, [value], [...args])](#module_model__set) ⇒ <code>Model</code>
        * [.parse([data], [...args])](#module_model__parse) ⇒ <code>object</code>
        * [.toJSON()](#module_model__tojson) ⇒ <code>object</code>
    * _static_
        * [.attributePrefix](#module_model_attributeprefix) : <code>string</code>
* [View](#module_view) ⇐ <code>Emitter</code>
    * _instance_
        * [.preinitialize(options)](#module_view__preinitialize)
        * [.$(selector)](#module_view__$) ⇒ <code>node</code>
        * [.$$(selector)](#module_view__$$) ⇒ <code>Array.&lt;node&gt;</code>
        * [.destroy(options)](#module_view__destroy) ⇒ <code>View</code>
        * [.onDestroy(options)](#module_view__ondestroy)
        * [.addChild(child)](#module_view__addchild) ⇒ <code>View</code>
        * [.destroyChildren()](#module_view__destroychildren)
        * [.ensureUid()](#module_view__ensureuid)
        * [.ensureElement()](#module_view__ensureelement)
        * [.createElement(tag, attributes)](#module_view__createelement) ⇒ <code>node</code>
        * [.removeElement()](#module_view__removeelement) ⇒ <code>View</code>
        * [.delegateEvents([events])](#module_view__delegateevents) ⇒ <code>View</code>
        * [.undelegateEvents()](#module_view__undelegateevents) ⇒ <code>View</code>
        * [.render()](#module_view__render) ⇒ <code>View</code>
    * _static_
        * [.uid](#module_view_uid) : <code>number</code>
        * [.sanitize(value)](#module_view_sanitize) ⇒ <code>string</code>
        * [.resetUid()](#module_view_resetuid)

## Typedefs

<dl>
<dt><a href="#componentpartial">ComponentPartial</a> : <code>object</code></dt>
<dd><p>An opaque partial template produced by <a href="#module_component__partial">component.partial</a>.
It carries a template&#39;s structure and current expressions so the render engine can
patch it in place.</p>
</dd>
</dl>

<a name="module_component" id="module_component" class="anchor"></a>
## Component ⇐ <code>View</code>
Components are a special kind of `View` that is designed to be easily composable,
making it simple to add child views and build complex user interfaces.
Unlike views, which are render-agnostic, components have a specific set of rendering
guidelines that allow for a more declarative development style.
A component renders from its [template](#module_component__template) method, which returns a partial (built with [partial](#module_component__partial)) or a child component. Let the [Component.create](#module_component_create) static method write it from a template function, the shortest form for presentational components; define it in a subclass, for components with methods or state; or pass `create` a tagged template string, whose interpolations are defined with the declaration, so dynamic values must be functions.

**Extends**: <code>View</code>  
**See**: [Component.create](#module_component_create)  

| Param | Type | Description |
| --- | --- | --- |
| options | <code>object</code> | Object containing options. The following keys will be merged to `this`: model, state, key, onDestroy, onHydrate, onBeforeRecycle, onRecycle, onBeforeUpdate, onUpdate, onCreate, onChange. Any additional options not in the component or view options list will be automatically extracted as props and stored as `this.props`. |

**Properties**

| Name | Type | Description |
| --- | --- | --- |
| [key] | <code>string</code> \| <code>number</code> | A unique key to identify the component. Components with keys are recycled when a previous child of the same class with the same key is found in the previous render of the same interpolation. Unkeyed components are recycled by type and position for a single value, but are never recycled inside an array. Keys are compared by identity, so `1` and `'1'` are different keys. |
| [model] | <code>Model</code> | A `Model` or any emitter object containing data and business logic. The component will listen to `change` events and call `onChange` lifecycle method. |
| [state] | <code>Model</code> | A `Model` or any emitter object containing data and business logic, to be used as internal state. The component will listen to `change` events and call `onChange` lifecycle method. |
| props | <code>Model</code> | Automatically created from any options not merged to the component instance. Contains props passed from parent component, to be read. A plain object in the constructor, then a `Model` from the first render on, built by [createProps](#module_component__createprops): the component will listen to `change` events on props and call `onChange` lifecycle method. When the component is recycled during a parent re-render, its props are set again and any change triggers a re-render. Props are compared by identity, so a function, object or array created during the parent's render counts as a change every time. |
| html | <code>function</code> | Alias of [partial](#module_component__partial), bound to the component instance. Not set when a subclass defines its own `html`. |

**Example**  
```js
import { Component, Model } from 'rasti';
// Create Timer component.
class Timer extends Component {
    template() {
        return this.html`
            <div>
                Seconds: <span>${this.model.seconds}</span>
            </div>
        `;
    }
}
// Create model to store seconds.
const model = new Model({ seconds : 0 });
// Mount timer on body.
Timer.mount({ model }, document.body);
// Increment `model.seconds` every second.
setInterval(() => model.seconds++, 1000);
```

* [Component](#module_component) ⇐ <code>View</code>
    * _instance_
        * [.createProps(props)](#module_component__createprops) ⇒ <code>Model</code>
        * [.provide()](#module_component__provide) ⇒ <code>object</code> \| <code>null</code>
        * [.inject(key)](#module_component__inject) ⇒ <code>Model</code>
        * [.subscribe(model, [type], [listener])](#module_component__subscribe) ⇒ <code>Component</code>
        * [.partial(strings, ...expressions)](#module_component__partial) ⇒ [<code>ComponentPartial</code>](#componentpartial)
        * [.template()](#module_component__template) ⇒ [<code>ComponentPartial</code>](#componentpartial) \| <code>Component</code>
        * [.toString()](#module_component__tostring) ⇒ <code>string</code>
        * [.render()](#module_component__render) ⇒ <code>Component</code>
        * [.onCreate(...args)](#module_component__oncreate)
        * [.onChange(model, changed)](#module_component__onchange)
        * [.onHydrate()](#module_component__onhydrate)
        * [.onBeforeRecycle()](#module_component__onbeforerecycle)
        * [.onRecycle()](#module_component__onrecycle)
        * [.onBeforeUpdate()](#module_component__onbeforeupdate)
        * [.onUpdate()](#module_component__onupdate)
        * [.onDestroy(...args)](#module_component__ondestroy)
    * _static_
        * [.markAsSafeHTML(value)](#module_component_markassafehtml) ⇒ <code>object</code>
        * [.extend(object)](#module_component_extend)
        * [.mount([options], [containerEl], [shouldHydrateExistingDOM])](#module_component_mount) ⇒ <code>Component</code>
        * [.create(strings, ...expressions)](#module_component_create) ⇒ <code>Component</code>

<a name="module_component__createprops" id="module_component__createprops" class="anchor"></a>
### component.createProps(props) ⇒ <code>Model</code>
Build the `Model` the component keeps its props in. Called once, on the first
render, with the options the parent passed that are not component or view
options; until then `this.props` is that plain object.

Override it to give props a model of your own — one with `defaults`, or one that
validates what a parent passes.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: <code>Model</code> - The model to keep the props in.  

| Param | Type | Description |
| --- | --- | --- |
| props | <code>object</code> | The props the component was mounted with. |

**Example**  
```js
const Button = Component.create`<button>${({ props }) => props.label}</button>`.extend({
    createProps(props) {
        return new ButtonProps(props);
    }
});
```
<a name="module_component__provide" id="module_component__provide" class="anchor"></a>
### component.provide() ⇒ <code>object</code> \| <code>null</code>
Provide contexts to the components this one hosts, at any depth: its children,
including the content slotted into it, and their own. Called on every render,
before the template's children render.

Return an object mapping each context key to the values of that context, or `null`
to provide nothing. Each key gets one `Model`, created on the first render and
`set` with the returned values on every later one, so return the same keys, with
the same properties, on every render (`null` for a value that is absent). Use
`Symbol` keys exported by a module of their own, so neither side imports the other.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: <code>object</code> \| <code>null</code> - The values of each context, by key.  
**Example**  
```js
import { Component } from 'rasti';
import { FormControl } from './form-control.js';

class Field extends Component {
    provide() {
        const { controlId, error } = this.props;
        return { [FormControl] : { id : controlId, invalid : !!error } };
    }
}
```
<a name="module_component__inject" id="module_component__inject" class="anchor"></a>
### component.inject(key) ⇒ <code>Model</code>
Read a context provided by a parent. The search starts at `this.parent` and walks
up the views that adopted this one, so a component that provides a key reads its
parent's, not its own. Bound to the component instance from the first render on.

Returns the `Model` of the nearest parent that provides `key`, or a shared, empty
and frozen `Model` when none does: its properties read `undefined`, so a consumer
falls back to its own defaults. The component subscribes to each provider's model
it reads, like it does to `props`, and re-renders when the provided values change.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: <code>Model</code> - The context model.  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>symbol</code> \| <code>string</code> | The context key. |

**Example**  
```js
import { Component } from 'rasti';
import { FormControl } from './form-control.js';

const Input = Component.create`
    <input id="${({ props, inject }) => props.id || inject(FormControl).id}" />
`;
```
<a name="module_component__subscribe" id="module_component__subscribe" class="anchor"></a>
### component.subscribe(model, [type], [listener]) ⇒ <code>Component</code>
Subscribes to a `change` event on a model or emitter object and invokes the `onChange` lifecycle method.
The subscription is automatically cleaned up when the component is destroyed.
By default, the component subscribes to changes on `this.model`, `this.state`, `this.props`,
and the contexts it reads with `inject`.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: <code>Component</code> - The current component instance for chaining.  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| model | <code>Object</code> |  | The model or emitter object to listen to. |
| [type] | <code>string</code> | <code>&quot;&#x27;change&#x27;&quot;</code> | The event type to listen for. |
| [listener] | <code>function</code> | <code>this.onChange</code> | The callback to invoke when the event is emitted. |

<a name="module_component__partial" id="module_component__partial" class="anchor"></a>
### component.partial(strings, ...expressions) ⇒ [<code>ComponentPartial</code>](#componentpartial)
Tagged template helper method.
Used to create a partial template.
It will return a `Partial`: the template's structure paired with this render's
expressions, which the component renders and then patches in place on later renders.
Components interpolated in it will be added as children by the parent component.
Template strings literals will be marked as safe HTML to be rendered.
This method is bound to the component instance by default, and is also available as `this.html`.

A partial used inside an interpolation may render any number of nodes, and may be
swapped for a different template between renders. The partial returned by
[template](#module_component__template) is the component's root and is
restricted on both counts.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: [<code>ComponentPartial</code>](#componentpartial) - The partial to render.  

| Param | Type | Description |
| --- | --- | --- |
| strings | <code>TemplateStringsArray</code> | Template strings. |
| ...expressions | <code>any</code> | Template expressions. |

**Example**  
```js
import { Component } from 'rasti';
// Create a Title component.
const Title = Component.create`
    <h1>${({ props }) => props.renderChildren()}</h1>
`;
// Create Main component.
const Main = Component.create`
    <main>
        ${self => self.renderHeader()}
    </main>
`.extend({
    // Render header method.
    // Use `partial` to render an HTML template adding children components.
    renderHeader() {
        return this.partial`
            <header>
                <${Title}>${({ model }) => model.title}</${Title}>
            </header>
        `;
    }
});
```
<a name="module_component__template" id="module_component__template" class="anchor"></a>
### component.template() ⇒ [<code>ComponentPartial</code>](#componentpartial) \| <code>Component</code>
Return the component's root partial. Called on every render, so interpolated
values are recomputed. Override it (directly, via `extend`, or through `create`)
to define the markup. It may also return a child component instance, in which case
the component is rendered as a <b>container</b> around it. A root partial that is
a single component tag (``this.html`<${Child} />` ``) makes a container too, and
builds the child only when it cannot recycle the previous one (see `render`). So
does returning the content slotted into it (`this.props.renderChildren()`), when
that content has a single root element: the component adds no element of its own,
and the components in that content are its children.

The base implementation builds the element the way a view does: from `this.tag`
(defaulting to `div`) and the component's `attributes`, rendering the content the
parent slotted into it (`props.renderChildren`). A void tag renders self-closed
and takes no content.

```javascript
// <section class="panel">Content</section>
Component.mount({
    tag : 'section',
    attributes : { class : 'panel' },
    renderChildren : () => 'Content'
}, document.body);
```

The root element is created on the first render and keeps its tag from then on:
later renders patch that element in place, so a `tag` that changes afterwards is
not applied — and one that changes between a void and a non-void tag throws, since
that does change the root template.

Two restrictions apply to the root partial, and only to it — the partials rendered
inside an interpolation are free of both:

- It must have a <b>single root element</b>, which becomes the component's `this.el`.
  Development builds throw when it resolves to anything else, since the nodes
  beside the root would be left out of every update.
- It must be built from the <b>same template</b> on every render, since the root is
  patched in place; returning a different one throws. Branch inside the interpolations
  instead of switching the root itself.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: [<code>ComponentPartial</code>](#componentpartial) \| <code>Component</code> - The root partial, or a child component to contain.  
<a name="module_component__tostring" id="module_component__tostring" class="anchor"></a>
### component.toString() ⇒ <code>string</code>
Render the component as a string.
Used internally on the render process.
Use it for server-side rendering or static site generation.

Rendering is the `toString` contract: every piece Rasti emits answers it with
the markup it stands for, so a component goes straight into any HTML string —
interpolated into a server-side layout, or concatenated with it.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: <code>string</code> - The rendered component.  
**Example**  
```js
import { Component } from 'rasti';
const Button = Component.create`
    <button class="button">Click me</button>
`;
const App = Component.create`
    <div>
        <${Button}>Click me</${Button}>
    </div>
`;

const app = new App();

console.log(app.toString());
// <div data-rst-el="r1-1"><!--rst-s-r1-1--><button class="button" data-rst-el="r2-1">Click me</button><!--rst-e-r1-1--></div>

console.log(`${app}`);
// <div data-rst-el="r1-1"><!--rst-s-r1-1--><button class="button" data-rst-el="r2-1">Click me</button><!--rst-e-r1-1--></div>
```
<a name="module_component__render" id="module_component__render" class="anchor"></a>
### component.render() ⇒ <code>Component</code>
Render the `Component`.

**First render (before the component is hydrated):**
This is the initial render call, where `template()` runs for the first time. The component will be rendered
as a string inside a `DocumentFragment` and hydrated, making `this.el` available. `this.el` is the root DOM
element of the component that can be applied to the DOM. If the component has its own `el`, it hydrates onto
that existing DOM instead (server-side rendered markup).
The `onHydrate` lifecycle method will be called.

**Note:** Typically, you don't need to call `render()` directly for the first render. The static method `Component.mount()`
handles this process automatically, creating the component instance, rendering it, and appending it to the DOM.

**Update render (once the component is hydrated):**
The component keeps its root partial and root element, and reconciles their
contents recursively instead of rendering the whole tree from scratch. Elements
and retained partials are patched in place, matching children are recycled, and
an interpolation region is regenerated only when its occupant changes identity
or shape. The method will:
- Diff and update the attributes of every element in the template and in its partials
- Reconcile the content of each interpolation (the dynamic parts of the template), updating nested partials in place
- For container components (components that render a single child component), update the single interpolation

The `onBeforeUpdate` lifecycle method will be called at the beginning, followed by the `onUpdate` lifecycle method at the end.

A render asked for while the component is rendering, its hydration included, does not interrupt it: it runs
once that render is done, after `onHydrate` or `onUpdate`.

**Child component handling:**
When rendering child components, they can be either recreated or recycled:

- **Recreation:** A new component instance is created, running the constructor again. This happens when no matching component
  is found for recycling.

- **Recycling:** The same component instance is reused. Recycling happens in two ways:
  - Unkeyed components are recycled if they have the same type and position in the template or partial
  - Components with a `key` are recycled if a previous child of the same class with the same key occupied the same interpolation

  A `key` identifies a component among the siblings of its own interpolation, which is what lets the items
  of a list be reordered without recreating them: ``items.map(item => html`<${Row} key="${item.id}" />`)``
  reuses the rows and moves them into place. Nested arrays are one list, so
  ``groups.map(group => group.map(…))`` does the same with its rows. A component inside any other partial,
  wrapped in markup (``html`<li><${Row} key="…" /></li>` ``) or not (``html`${Row.mount({ key : … })}` ``),
  belongs to that partial instead, and is recreated when the list reorders. Development builds warn about it.

  When a component is recycled:
  - The `onBeforeRecycle` lifecycle method is called when recycling starts
  - The component's `this.props` is updated with the new props from the parent
  - The `onRecycle` lifecycle method is called after props are updated

  A recycled component may not use props at all and remain unchanged, or it may be subscribed to a different model
  (or even the same model as the parent) and update independently in subsequent render cycles.

- **Component tags and built components:** a child written as a component tag (``html`<${Row} key="…" />` ``)
  is built only when it is recreated: when the render recycles a previous child instead, the tag's attributes go
  straight to it as props, and no new instance is created. A child an expression builds
  (`Row.mount(options)` or `new Row(options)`) is a new instance on every render, so its constructor and
  `onCreate` run each time; when the render recycles the previous child in its place, the new instance takes no
  part in the render: its props are handed to the previous one and it is destroyed, running `onDestroy`.
  Both forms recycle the same way. Prefer the tag: it does less work on every update.

**Kind**: instance method of [<code>Component</code>](#module_component)  
**Returns**: <code>Component</code> - The component instance.  
<a name="module_component__oncreate" id="module_component__oncreate" class="anchor"></a>
### component.onCreate(...args)
Lifecycle method. Called when the component is created, at the end of the constructor.
This method receives the same arguments passed to the constructor (options and any additional parameters).
It executes both on client and server.
Use this method to define models or state that will be used later in `onHydrate`.

A child written as a component tag is only created when the parent's render does not
recycle a previous one. A child built in an expression (`Child.mount(options)` or
`new Child(options)`) is created on every render of its parent, and when the
previous one is recycled instead, the new one is destroyed without ever rendering.
Keep `onCreate` free of side effects that outlive the instance.

**Kind**: instance method of [<code>Component</code>](#module_component)  

| Param | Type | Description |
| --- | --- | --- |
| ...args | <code>\*</code> | The constructor arguments (options and any additional parameters). |

<a name="module_component__onchange" id="module_component__onchange" class="anchor"></a>
### component.onChange(model, changed)
Lifecycle method. Called when model emits `change` event.
By default calls `render` method.
This method can be extended with custom logic.
Maybe comparing new attributes with previous ones and calling
render when needed.

**Kind**: instance method of [<code>Component</code>](#module_component)  

| Param | Type | Description |
| --- | --- | --- |
| model | <code>Model</code> | The model that emitted the event. |
| changed | <code>object</code> | Object containing keys and values that has changed. |
| [...args] | <code>any</code> | Any extra arguments passed to set method. |

<a name="module_component__onhydrate" id="module_component__onhydrate" class="anchor"></a>
### component.onHydrate()
Lifecycle method. Called when the component is rendered for the first time and hydrated.
Its DOM is already in place: in the container it was mounted into, in the existing DOM it hydrated, or in
its parent's DOM when an update added it. A component rendered with `render()` alone hydrates in a detached
`DocumentFragment`.
A model written here reaches the components already hydrated, its ancestors included, which render once the
render in progress is done; the components hydrated after this one rendered the previous value, and show it
until the next change.
This method only executes on the client and only during the first render.
Use this method for client-only operations like making API requests or setting up browser-specific functionality.

**Kind**: instance method of [<code>Component</code>](#module_component)  
<a name="module_component__onbeforerecycle" id="module_component__onbeforerecycle" class="anchor"></a>
### component.onBeforeRecycle()
Lifecycle method. Called before the component is recycled and reused between renders.
This method is called at the beginning of the `recycle` method, before any recycling operations occur.

A component is recycled when:
- It has a `key` and a previous child of the same class with the same key exists
- It doesn't have a `key` but has the same type and position in the template or partial

Use this method to perform operations that need to happen before the component is recycled,
such as storing previous state or preparing for the recycling.

**Kind**: instance method of [<code>Component</code>](#module_component)  
<a name="module_component__onrecycle" id="module_component__onrecycle" class="anchor"></a>
### component.onRecycle()
Lifecycle method. Called when the component is recycled and reused between renders.

A component is recycled when:
- It has a `key` and a previous child of the same class with the same key exists
- It doesn't have a `key` but has the same type and position in the template or partial

During recycling, the component instance is reused and its props are updated with new values.
The component's element may be moved in the DOM if the new template structure differs from the previous one.

**Kind**: instance method of [<code>Component</code>](#module_component)  
<a name="module_component__onbeforeupdate" id="module_component__onbeforeupdate" class="anchor"></a>
### component.onBeforeUpdate()
Lifecycle method. Called before the component is updated or re-rendered.
This method is called at the beginning of the `render` method when the component's state, model, or props change and trigger a re-render.
Use this method to perform operations that need to happen before the component is updated,
such as saving previous state or preparing for the update.

**Kind**: instance method of [<code>Component</code>](#module_component)  
<a name="module_component__onupdate" id="module_component__onupdate" class="anchor"></a>
### component.onUpdate()
Lifecycle method. Called when the component is updated or re-rendered.
This method is called when the component's state, model, or props change and trigger a re-render.
Use this method to perform operations that need to happen on every update.

**Kind**: instance method of [<code>Component</code>](#module_component)  
<a name="module_component__ondestroy" id="module_component__ondestroy" class="anchor"></a>
### component.onDestroy(...args)
Lifecycle method. Called when the component is destroyed.
Use this method to clean up resources, cancel timers, remove event listeners, etc.
It also runs for a child built in an expression (`Child.mount(options)` or
`new Child(options)`) that the parent's render discards because it recycles the
previous one (see `render`).

**Kind**: instance method of [<code>Component</code>](#module_component)  

| Param | Type | Description |
| --- | --- | --- |
| ...args | <code>\*</code> | Options object or any arguments passed to `destroy` method. |

<a name="module_component_markassafehtml" id="module_component_markassafehtml" class="anchor"></a>
### Component.markAsSafeHTML(value) ⇒ <code>object</code>
Mark a string as safe HTML to be rendered.
Normally you don't need to use this method, as Rasti will automatically mark string literals
as safe HTML when the component is [created](#module_component_create) and when
using the [Component.partial](#module_component__partial) method.
Be sure that the string is safe to be rendered, as it will be inserted into the DOM without any sanitization.

**Kind**: static method of [<code>Component</code>](#module_component)  
**Returns**: <code>object</code> - An opaque safe HTML value.  

| Param | Type |
| --- | --- |
| value | <code>string</code> | 

<a name="module_component_extend" id="module_component_extend" class="anchor"></a>
### Component.extend(object)
Helper method used to extend a `Component`, creating a subclass.

**Kind**: static method of [<code>Component</code>](#module_component)  

| Param | Type | Description |
| --- | --- | --- |
| object | <code>object</code> \| <code>function</code> | Object containing methods to be added to the new `Component` subclass. Also can be a function that receives the parent prototype and returns an object. |

<a name="module_component_mount" id="module_component_mount" class="anchor"></a>
### Component.mount([options], [containerEl], [shouldHydrateExistingDOM]) ⇒ <code>Component</code>
Mount the component into the DOM.
Creates a new component instance with the provided options and optionally mounts it into the DOM.

**Mounting modes:**
- **Normal mount** (default): Renders the component as HTML and appends it to the provided element. Use this for client-side rendering.
- **Hydration mode**: Assumes the DOM already contains the component's HTML (from server-side rendering).

If `containerEl` is not provided, the component is instantiated but not mounted (the same as using `new Component(options)`). You can mount it later by calling `render()` and appending the element (`this.el`) to the DOM.

**Kind**: static method of [<code>Component</code>](#module_component)  
**Returns**: <code>Component</code> - The component instance.  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [options] | <code>object</code> | <code>{}</code> | The component options. These will be passed to the constructor and can include                              `model`, `state`, `props`, lifecycle methods, and any other component-specific options. |
| [containerEl] | <code>node</code> |  | The DOM element where the component will be mounted. If provided, the component                             will be rendered and appended to this element. If not provided, the component is                             created but not mounted. A component that has its own `el` (an option, a class                             field, or a function that returns it) hydrates onto that element instead, as                             `render()` does, and the container is not used. |
| [shouldHydrateExistingDOM] | <code>boolean</code> | <code>false</code> | If `true`, enables hydration mode for server-side rendering. The                                                   component will assume the DOM already contains its HTML structure                                                   and will only hydrate it. If `false` (default), the component will                                                   be rendered from scratch and appended to `containerEl`. |

**Example**  
```js
import { Component, Model } from 'rasti';

const Button = Component.create`
    <button class="${({ props }) => props.className}">
        ${({ props }) => props.label}
    </button>
`;

// Normal mount: render and append to DOM.
const button = Button.mount({
    label: 'Click me'
}, document.body);

// Create without mounting (mount later).
const button2 = Button.mount({ className : 'secondary', label : 'Save' });
// Later, render and append it to the DOM.
document.body.appendChild(button2.render().el);

// Hydration mode: hydrate existing server-rendered HTML
// Assuming document.body already contains the HTML structure of the button.
const hydratedButton = Button.mount({
    className : 'primary',
    label : 'Click me'
}, document.body, true);
```
<a name="module_component_create" id="module_component_create" class="anchor"></a>
### Component.create(strings, ...expressions) ⇒ <code>Component</code>
Takes a template function, or a tagged template string, and returns a new `Component` class. Both are sugar for defining the component's [template](#module_component__template) method. A component with methods, state or several hooks reads better as a subclass that defines `template()` itself; `create` suits presentational components.
- **Template function**: the function is used as `template()` itself, so it re-runs on
  every render and interpolates plain values. It is called with the component as its
  argument, so an arrow function destructures what it uses, and a `function` reads `this`.
  It returns a partial, or a component to contain.
  ```javascript
  const Button = Component.create(({ props, html }) => html`
      <button class="${props.className}">
          ${props.renderChildren()}
      </button>
  `);
  ```
- **Tagged template**: the template becomes the component's root partial, and its outer
  element becomes `this.el`. Its interpolations are defined once, with the declaration, so
  anything dynamic must be a function: it is called on every render, bound to the
  component and receiving it as its argument. That pays off in a few specific cases. A
  function that returns `null`, `undefined`, `false` or an empty string renders nothing.
  ```javascript
  const Row = Component.create`
      <tr class="${({ props }) => props.selected ? 'selected' : ''}">
          <td>${({ model }) => model.label}</td>
          <td><a onClick=${function() { this.props.handleRemove(this.model); }}>Remove</a></td>
      </tr>
  `;
  ```
- **Attributes**: a quoted value that is a function is called and its result used; an
  unquoted one is passed as it is. A boolean decides whether the attribute is present.
  ```javascript
  const Input = Component.create(({ props, html }) => html`
      <input type="text" disabled="${props.disabled}" />
  `);
  ```
- **Event handlers**: camel-cased `on*` attributes bind a function, or a string naming a
  method of the component. The handler runs with `this` bound to the component and
  receives `(event, component, matched)`, `matched` being the element that carries the
  attribute. Handlers are not bound to the elements: one listener per event type is
  delegated to the component's root element and looks the handler up on each event, so a
  handler created anew on every render touches no DOM. On a component tag, an attribute
  is an option for the child, not a listener: name callback props `handle*`, since
  lifecycle hook names such as `onChange` are options the component takes for itself.
  ```javascript
  const Counter = Component.create(({ model, html }) => html`
      <div>
          <span>${model.count}</span>
          <button onClick=${() => model.count++}>+</button>
          <button onClick="reset">Reset</button>
      </div>
  `).extend({
      reset() {
          this.model.count = 0;
      }
  });
  ```
  If you need custom delegation (e.g., `{'click .selector': 'handler'}`),
  you may override the `events` property as described in [View.delegateEvents](#module_view__delegateevents).
- **Child components** are written as tags: attributes become props, and the content
  between the tags becomes `props.renderChildren`. In a list, give each one a `key`, so it
  is reused and moved instead of built again on every update.
  ```javascript
  const Navigation = Component.create(({ props, html }) => html`
      <nav>
          ${props.items.map(item => html`
              <${Button} key="${item.id}" className="nav">${item.label}</${Button}>
          `)}
      </nav>
  `);
  ```
- **Containers**: a template that is a single component makes the component a
  <b>container</b>: `this.el` is the child's element, with no wrapper around it.
  ```javascript
  const ButtonOk = Component.create`
      <${Button} className="ok">Ok</${Button}>
  `;
  const ButtonCancel = Component.create(({ html }) => html`
      <${Button} className="cancel">Cancel</${Button}>
  `);
  ```
  The function may also return a component it builds (`Button.mount(options)`), which is
  then built on every render (see [render](#module_component__render)).
- **Called on a subclass**, the new component extends it, so the template can use its methods.
  ```javascript
  class ListBase extends Component {
      renderItems() {
          return this.props.items.map(item => this.html`<li>${item}</li>`);
      }
  }
  const List = ListBase.create`
      <ul>${(self) => self.renderItems()}</ul>
  `;
  ```

**Kind**: static method of [<code>Component</code>](#module_component)  
**Returns**: <code>Component</code> - The newly created component class.  

| Param | Type | Description |
| --- | --- | --- |
| strings | <code>function</code> \| <code>string</code> | A template function that returns a partial or a child component, or a tagged template string for the component. |
| ...expressions | <code>\*</code> | The expressions to be interpolated within the template. |

<a name="module_emitter" id="module_emitter" class="anchor"></a>
## Emitter
`Emitter` is a class that provides an easy way to implement the observer pattern 
in your applications.  
It can be extended to create new classes that have the ability to emit and bind custom named events.   
Emitter is used by `Model` and `View` classes, which inherit from it to implement 
event-driven functionality.

## Inverse of Control Pattern

The Emitter class includes "inverse of control" methods (`listenTo`, `listenToOnce`, `stopListening`) 
that allow an object to manage its own listening relationships. Instead of:

```javascript
// Traditional approach - harder to clean up
otherObject.on('change', this.myHandler);
otherObject.on('destroy', this.cleanup);
// Later you need to remember to clean up each listener
otherObject.off('change', this.myHandler);
otherObject.off('destroy', this.cleanup);
```

You can use:

```javascript
// Inverse of control - easier cleanup
this.listenTo(otherObject, 'change', this.myHandler);
this.listenTo(otherObject, 'destroy', this.cleanup);
// Later, clean up ALL listeners at once
this.stopListening(); // Removes all listening relationships
```

This pattern is particularly useful for preventing memory leaks and simplifying cleanup
in component lifecycle management.

**Example**  
```js
import { Emitter } from 'rasti';
// Custom cart
class ShoppingCart extends Emitter {
    constructor() {
        super();
        this.items = [];
    }

    addItem(item) {
        this.items.push(item);
        // Emit a custom event called `itemAdded`.
        // Pass the added item as an argument to the event listener.
        this.emit('itemAdded', item);
    }
}
// Create an instance of ShoppingCart and Logger
const cart = new ShoppingCart();
// Listen to the `itemAdded` event and log the added item using the logger.
cart.on('itemAdded', (item) => {
    console.log(`Item added to cart: ${item.name} - Price: $${item.price}`);
});
// Simulate adding items to the cart
const item1 = { name : 'Smartphone', price : 1000 };
const item2 = { name : 'Headphones', price : 150 };

cart.addItem(item1); // Output: "Item added to cart: Smartphone - Price: $1000"
cart.addItem(item2); // Output: "Item added to cart: Headphones - Price: $150"
```

* [Emitter](#module_emitter)
    * [.on(type, listener)](#module_emitter__on) ⇒ <code>function</code>
    * [.once(type, listener)](#module_emitter__once) ⇒ <code>function</code>
    * [.off([type], [listener])](#module_emitter__off)
    * [.emit(type, [...args])](#module_emitter__emit)
    * [.listenTo(emitter, type, listener)](#module_emitter__listento) ⇒ <code>function</code>
    * [.listenToOnce(emitter, type, listener)](#module_emitter__listentoonce) ⇒ <code>function</code>
    * [.stopListening([emitter], [type], [listener])](#module_emitter__stoplistening)

<a name="module_emitter__on" id="module_emitter__on" class="anchor"></a>
### emitter.on(type, listener) ⇒ <code>function</code>
Adds event listener.

**Kind**: instance method of [<code>Emitter</code>](#module_emitter)  
**Returns**: <code>function</code> - A function to remove the listener.  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | Type of the event (e.g. `change`). |
| listener | <code>function</code> | Callback function to be called when the event is emitted. |

**Example**  
```js
// Re render when model changes.
this.model.on('change', this.render.bind(this));
```
<a name="module_emitter__once" id="module_emitter__once" class="anchor"></a>
### emitter.once(type, listener) ⇒ <code>function</code>
Adds event listener that executes once.

**Kind**: instance method of [<code>Emitter</code>](#module_emitter)  
**Returns**: <code>function</code> - A function to remove the listener.  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | Type of the event (e.g. `change`). |
| listener | <code>function</code> | Callback function to be called when the event is emitted. |

**Example**  
```js
// Log a message once when model changes.
this.model.once('change', () => console.log('This will happen once'));
```
<a name="module_emitter__off" id="module_emitter__off" class="anchor"></a>
### emitter.off([type], [listener])
Removes event listeners with flexible parameter combinations.

**Kind**: instance method of [<code>Emitter</code>](#module_emitter)  

| Param | Type | Description |
| --- | --- | --- |
| [type] | <code>string</code> | Type of the event (e.g. `change`). If not provided, removes ALL listeners from this emitter. |
| [listener] | <code>function</code> | Specific callback function to remove. If not provided, removes all listeners for the specified type. **Behavior based on parameters:** - `off()` - Removes ALL listeners from this emitter - `off(type)` - Removes all listeners for the specified event type - `off(type, listener)` - Removes the specific listener for the specified event type |

**Example**  
```js
// Remove all listeners from this emitter
this.model.off();
```
**Example**  
```js
// Remove all 'change' event listeners
this.model.off('change');
```
**Example**  
```js
// Remove specific listener for 'change' events
const myListener = () => console.log('changed');
this.model.on('change', myListener);
this.model.off('change', myListener);
```
<a name="module_emitter__emit" id="module_emitter__emit" class="anchor"></a>
### emitter.emit(type, [...args])
Emits event of specified type. Listeners will receive specified arguments.

**Kind**: instance method of [<code>Emitter</code>](#module_emitter)  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | Type of the event (e.g. `change`). |
| [...args] | <code>any</code> | Optional arguments to be passed to listeners. |

**Example**  
```js
// Emit validation error event with no arguments
this.emit('invalid');
```
**Example**  
```js
// Emit change event with data
this.emit('change', { field : 'name', value : 'John' });
```
<a name="module_emitter__listento" id="module_emitter__listento" class="anchor"></a>
### emitter.listenTo(emitter, type, listener) ⇒ <code>function</code>
Listen to an event of another emitter (Inverse of Control pattern).

This method allows this object to manage its own listening relationships,
making cleanup easier and preventing memory leaks. Instead of calling
`otherEmitter.on()`, you call `this.listenTo(otherEmitter, ...)` which
allows this object to track and clean up all its listeners at once.

**Kind**: instance method of [<code>Emitter</code>](#module_emitter)  
**Returns**: <code>function</code> - A function to stop listening to the event.  

| Param | Type | Description |
| --- | --- | --- |
| emitter | <code>Emitter</code> | The emitter to listen to. |
| type | <code>string</code> | The type of the event to listen to. |
| listener | <code>function</code> | The listener to call when the event is emitted. |

**Example**  
```js
// Instead of: otherModel.on('change', this.render.bind(this));
// Use: this.listenTo(otherModel, 'change', this.render.bind(this));
// This way you can later call this.stopListening() to clean up all listeners
```
<a name="module_emitter__listentoonce" id="module_emitter__listentoonce" class="anchor"></a>
### emitter.listenToOnce(emitter, type, listener) ⇒ <code>function</code>
Listen to an event of another emitter and remove the listener after it is called (Inverse of Control pattern).

Similar to `listenTo()` but automatically removes the listener after the first execution,
like `once()` but with the inverse of control benefits for cleanup management.

**Kind**: instance method of [<code>Emitter</code>](#module_emitter)  
**Returns**: <code>function</code> - A function to stop listening to the event.  

| Param | Type | Description |
| --- | --- | --- |
| emitter | <code>Emitter</code> | The emitter to listen to. |
| type | <code>string</code> | The type of the event to listen to. |
| listener | <code>function</code> | The listener to call when the event is emitted. |

**Example**  
```js
// Listen once to another emitter's initialization event
this.listenToOnce(otherModel, 'initialized', () => {
    console.log('Other model initialized');
});
```
<a name="module_emitter__stoplistening" id="module_emitter__stoplistening" class="anchor"></a>
### emitter.stopListening([emitter], [type], [listener])
Stop listening to events from other emitters (Inverse of Control pattern).

This method provides flexible cleanup of listening relationships established with `listenTo()`.
All parameters are optional, allowing different levels of cleanup granularity.

**Kind**: instance method of [<code>Emitter</code>](#module_emitter)  

| Param | Type | Description |
| --- | --- | --- |
| [emitter] | <code>Emitter</code> | The emitter to stop listening to. If not provided, stops listening to ALL emitters. |
| [type] | <code>string</code> | The type of event to stop listening to. If not provided, stops listening to all event types from the specified emitter. |
| [listener] | <code>function</code> | The specific listener to remove. If not provided, removes all listeners for the specified event type from the specified emitter. **Behavior based on parameters:** - `stopListening()` - Stops listening to ALL events from ALL emitters - `stopListening(emitter)` - Stops listening to all events from the specified emitter - `stopListening(emitter, type)` - Stops listening to the specified event type from the specified emitter - `stopListening(emitter, type, listener)` - Stops listening to the specific listener for the specific event from the specific emitter |

**Example**  
```js
// Stop listening to all events from all emitters (complete cleanup)
this.stopListening();
```
**Example**  
```js
// Stop listening to all events from a specific emitter
this.stopListening(otherModel);
```
**Example**  
```js
// Stop listening to 'change' events from a specific emitter
this.stopListening(otherModel, 'change');
```
**Example**  
```js
// Stop listening to a specific listener
const myListener = () => console.log('changed');
this.listenTo(otherModel, 'change', myListener);
this.stopListening(otherModel, 'change', myListener);
```
<a name="module_model" id="module_model" class="anchor"></a>
## Model ⇐ <code>Emitter</code>
- Orchestrates data and business logic.
- Emits events when data changes.

A `Model` manages an internal table of data attributes and triggers change events when any of its data is modified.  
Models may handle syncing data with a persistence layer. To design your models, create atomic, reusable objects 
that contain all the necessary functions for manipulating their specific data.  
Models should be easily passed throughout your app and used anywhere the corresponding data is needed.

## Construction Flow
1. `preinitialize()` is called with all constructor arguments
2. `this.defaults` are resolved (if function, it's called and bound to the model)
3. `parse()` is called with all constructor arguments to process the data
4. `this.attributes` is built by merging defaults and parsed data
5. Getters/setters are generated for each attribute to emit change events

**Extends**: <code>Emitter</code>  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [attributes] | <code>object</code> | <code>{}</code> | Primary data object containing model attributes |
| [...args] | <code>\*</code> |  | Additional arguments passed to `preinitialize` and `parse` methods |

**Properties**

| Name | Type | Description |
| --- | --- | --- |
| defaults | <code>object</code> \| <code>function</code> | Default attributes for the model. If a function, it's called bound to the model instance to get defaults. |
| previous | <code>object</code> | Object containing previous attributes when a change occurs. |

**Example**  
```js
import { Model } from 'rasti';

// User model
class User extends Model {
    preinitialize() {
        this.defaults = { name : '', email : '', role : 'user' };
    }
}
// Order model with nested User and custom methods
class Order extends Model {
    preinitialize(attributes, options = {}) {
        this.defaults = {
            id : null,
            total : 0,
            status : 'pending',
            user : null
        };

        this.apiUrl = options.apiUrl || '/api/orders';
    }

    parse(data, options = {}) {
        const parsed = { ...data };
        
        // Convert user object to User model instance
        if (data.user && !(data.user instanceof User)) {
            parsed.user = new User(data.user);
        }

        return parsed;
    }

    toJSON() {
        const result = {};
        for (const [key, value] of Object.entries(this.attributes)) {
            if (value instanceof Model) {
                result[key] = value.toJSON();
            } else {
                result[key] = value;
            }
        }
        return result;
    }

    async fetch() {
        try {
            const response = await fetch(`${this.apiUrl}/${this.id}`);
            const data = await response.json();
            
            // Parse the fetched data and update model
            const parsed = this.parse(data);
            this.set(parsed, { source : 'fetch' });

            return this;
        } catch (error) {
            console.error('Failed to fetch order:', error);
            throw error;
        }
    }
}

// Create order with nested user data
const order = new Order({
    id : 123,
    total : 99.99,
    user : { name : 'Alice', email : 'alice@example.com' }
});

console.log(order.user instanceof User); // true
// Serialize with nested models
const json = order.toJSON();
console.log(json); // { id: 123, total: 99.99, status: 'pending', user: { name: 'Alice', email: 'alice@example.com', role: 'user' } }

// Listen to fetch updates
order.on('change', (model, changed, options) => {
    if (options?.source === 'fetch') {
        console.log('Order updated from server:', changed);
    }
});

// Fetch latest data from server
await order.fetch();
```

* [Model](#module_model) ⇐ <code>Emitter</code>
    * _instance_
        * [.preinitialize([attributes], [...args])](#module_model__preinitialize)
        * [.defineAttribute(key)](#module_model__defineattribute)
        * [.get(key)](#module_model__get) ⇒ <code>any</code>
        * [.set(key, [value], [...args])](#module_model__set) ⇒ <code>Model</code>
        * [.parse([data], [...args])](#module_model__parse) ⇒ <code>object</code>
        * [.toJSON()](#module_model__tojson) ⇒ <code>object</code>
    * _static_
        * [.attributePrefix](#module_model_attributeprefix) : <code>string</code>

<a name="module_model__preinitialize" id="module_model__preinitialize" class="anchor"></a>
### model.preinitialize([attributes], [...args])
Called before any instantiation logic runs for the Model.
Receives all constructor arguments, allowing for flexible initialization patterns.
Use this to set up `defaults`, configure the model, or handle custom constructor arguments.

**Kind**: instance method of [<code>Model</code>](#module_model)  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [attributes] | <code>object</code> | <code>{}</code> | Primary data object containing model attributes |
| [...args] | <code>\*</code> |  | Additional arguments passed from the constructor |

**Example**  
```js
class User extends Model {
    preinitialize(attributes, options = {}) {
        this.defaults = { name : '', role : options.defaultRole || 'user' };
        this.apiEndpoint = options.apiEndpoint || '/users';
    }
}
const user = new User({ name : 'Alice' }, { defaultRole : 'admin', apiEndpoint : '/api/users' });
```
<a name="module_model__defineattribute" id="module_model__defineattribute" class="anchor"></a>
### model.defineAttribute(key)
Generate getter/setter for the given attribute key to emit `change` events.
The property name uses `attributePrefix` + key (e.g., with prefix 'attr_', key 'name' becomes 'attr_name').
Called internally by the constructor for each key in `this.attributes`.
Override with an empty method if you don't want automatic getters/setters.

The accessors are generated once for each class and set of attributes, on a
prototype shared by every model holding them, so they are inherited rather than
own properties of the model. A class shares them for up to 256 sets; a model
holding any other set gets a prototype of its own.

**Kind**: instance method of [<code>Model</code>](#module_model)  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>string</code> | Attribute key from `this.attributes` |

**Example**  
```js
// Custom prefix for all attributes
class PrefixedModel extends Model {
    static attributePrefix = 'attr_';
}
const model = new PrefixedModel({ name: 'Alice' });
console.log(model.attr_name); // 'Alice'

// Disable automatic getters/setters
class ManualModel extends Model {
    defineAttribute() {
        // Empty - no getters/setters generated
    }
    
    getName() {
        return this.get('name'); // Manual getter
    }
}
```
<a name="module_model__get" id="module_model__get" class="anchor"></a>
### model.get(key) ⇒ <code>any</code>
Get an attribute from `this.attributes`.
This method is called internally by generated getters.

**Kind**: instance method of [<code>Model</code>](#module_model)  
**Returns**: <code>any</code> - The attribute value.  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>string</code> | Attribute key. |

<a name="module_model__set" id="module_model__set" class="anchor"></a>
### model.set(key, [value], [...args]) ⇒ <code>Model</code>
Set one or more attributes into `this.attributes` and emit change events.
Supports two call signatures: `set(key, value, ...args)` or `set(object, ...args)`.
Additional arguments are passed to change event listeners, enabling custom behavior.

A `set` called from a `change:attribute` listener joins the `change` event of the
`set` that triggered it: that event is emitted once, after every `change:attribute`,
with all the attributes changed in between and the arguments of the first `set`.
A `set` called from a `change` listener emits a `change` event of its own.

**Kind**: instance method of [<code>Model</code>](#module_model)  
**Returns**: <code>Model</code> - This model instance for chaining  
**Emits**: <code>change Emitted when any attribute changes. Listeners receive &#x60;(model, changedAttributes, ...event:args)&#x60;</code>, <code>change:attribute Emitted for each changed attribute. Listeners receive &#x60;(model, newValue, ...event:args)&#x60;</code>  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>string</code> \| <code>object</code> | Attribute key (string) or object containing key-value pairs |
| [value] | <code>\*</code> | Attribute value (when key is string) |
| [...args] | <code>\*</code> | Additional arguments passed to event listeners |

**Example**  
```js
// Basic usage
model.set('name', 'Alice');
model.set({ name : 'Alice', age : 30 });

// With options for listeners
model.set('name', 'Bob', { silent : false, validate : true });
model.on('change:name', (model, value, options) => {
    if (options?.validate) {
        // Custom validation logic
    }
});
```
<a name="module_model__parse" id="module_model__parse" class="anchor"></a>
### model.parse([data], [...args]) ⇒ <code>object</code>
Transforms and validates data before it becomes model attributes.
Called during construction with all constructor arguments, allowing flexible data processing.
Override this method to transform incoming data, create nested models, or handle different data formats.

**Kind**: instance method of [<code>Model</code>](#module_model)  
**Returns**: <code>object</code> - Processed data that will become the model's attributes  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [data] | <code>object</code> | <code>{}</code> | Primary data object to be parsed into attributes |
| [...args] | <code>\*</code> |  | Additional arguments from constructor, useful for parsing options |

**Example**  
```js
// Transform nested objects into models
class User extends Model {}
class Order extends Model {
    parse(data, options = {}) {
        // Skip parsing if requested
        if (options.raw) return data;
        // Transform user data into User model
        const parsed = { ...data };
        if (data.user && !(data.user instanceof User)) {
            parsed.user = new User(data.user);
        }
        return parsed;
    }
}

// Usage with parsing options
const order1 = new Order({ id : 1, user : { name : 'Alice' } }); // user becomes User model
const order2 = new Order({ id : 2, user : { name : 'Bob' } }, { raw : true }); // user stays plain object
```
<a name="module_model__tojson" id="module_model__tojson" class="anchor"></a>
### model.toJSON() ⇒ <code>object</code>
Return object representation of the model to be used for JSON serialization.
By default returns a copy of `this.attributes`.
You can override this method to customize serialization behavior, such as calling `toJSON` recursively on nested Model instances.

**Kind**: instance method of [<code>Model</code>](#module_model)  
**Returns**: <code>object</code> - Object representation of the model to be used for JSON serialization.  
**Example**  
```js
// Basic usage - returns a copy of model attributes:
const user = new Model({ name : 'Alice', age : 30 });
const json = user.toJSON();
console.log(json); // { name : 'Alice', age : 30 }

// Override toJSON for recursive serialization of nested models:
class User extends Model {}
class Order extends Model {
    parse(data) {
        // Ensure user is always a User model
        return { ...data, user : data.user instanceof User ? data.user : new User(data.user) };
    }

    toJSON() {
        const result = {};
        for (const [key, value] of Object.entries(this.attributes)) {
            if (value instanceof Model) {
                result[key] = value.toJSON();
            } else {
                result[key] = value;
            }
        }
        return result;
    }
}
const order = new Order({ id : 1, user : { name : 'Alice' } });
const json = order.toJSON();
console.log(json); // { id : 1, user : { name : 'Alice' } }
```
<a name="module_model_attributeprefix" id="module_model_attributeprefix" class="anchor"></a>
### Model.attributePrefix : <code>string</code>
Static property that defines a prefix for generated getters/setters.
When set, all attribute properties will be prefixed (e.g., 'attr_name' instead of 'name').
Useful for avoiding naming conflicts or creating a consistent property naming convention.

**Kind**: static property of [<code>Model</code>](#module_model)  
**Default**: <code>&quot;&#x27;&#x27;&quot;</code>  
**Example**  
```js
// Set prefix for all models of this class
class ApiModel extends Model {
    static attributePrefix = 'attr_';
}

const user = new ApiModel({ name : 'Alice', email : 'alice@example.com' });
console.log(user.attr_name);  // 'Alice'
console.log(user.attr_email); // 'alice@example.com'

// Still access via get/set methods without prefix
console.log(user.get('name')); // 'Alice'
user.set('name', 'Bob');
console.log(user.attr_name); // 'Bob'
```
<a name="module_view" id="module_view" class="anchor"></a>
## View ⇐ <code>Emitter</code>
- Listens for changes and renders the UI.
- Handles user input and interactivity.
- Sends captured input to the model.

A `View` is an atomic unit of the user interface that can render data from a specific model or multiple models.
However, views can also be independent and have no associated data.  
Models must be unaware of views. Views, on the other hand, may render model data and listen to the change events 
emitted by the models to re-render themselves based on changes.  
Each `View` has a root element, `this.el`, which is used for event delegation.  
All element lookups are scoped to this element, and any rendering or DOM manipulations should be done inside it. 
If `this.el` is not present, an element will be created using `this.tag` (defaulting to `div`) and `this.attributes`.

**Extends**: <code>Emitter</code>  

| Param | Type | Description |
| --- | --- | --- |
| options | <code>object</code> | Object containing options. The following keys will be merged into the view instance: `el`, `tag`, `attributes`, `events`, `model`, `template`, `onDestroy`. |

**Properties**

| Name | Type | Description |
| --- | --- | --- |
| el | <code>node</code> \| <code>function</code> | Every view has a root DOM element stored at `this.el`. If not present, it will be created. If `this.el` is a function, it will be called to get the element at `this.ensureElement`, bound to the view instance. See [View.ensureElement](#module_view__ensureelement). |
| tag | <code>string</code> \| <code>function</code> | If `this.el` is not present, an element will be created using `this.tag` and `this.attributes`. Default is `div`. If it is a function, it will be called to get the tag, bound to the view instance. See [View.ensureElement](#module_view__ensureelement). |
| attributes | <code>object</code> \| <code>function</code> | If `this.el` is not present, an element will be created using `this.tag` and `this.attributes`. If it is a function, it will be called to get the attributes object, bound to the view instance. See [View.ensureElement](#module_view__ensureelement). |
| events | <code>object</code> \| <code>function</code> | Object in the format `{'event selector' : 'listener'}`. It will be used to bind delegated event listeners to the root element. If it is a function, it will be called to get the events object, bound to the view instance. See [View.delegateEvents](#module_view__delegateevents). |
| model | <code>object</code> | A model or any object containing data and business logic. |
| template | <code>function</code> | A function that returns a string with the view's inner HTML. See [View.render](#module_view__render). |
| children | <code>Array.&lt;View&gt;</code> | Child views, added with `addChild` and destroyed with the view. |
| parent | <code>View</code> \| <code>null</code> | The view that adopted this one with `addChild`. `null` before that, for a view mounted at the root, and after `destroy`. For a component's slotted content it is the component that renders it, not the one whose template wrote it. |
| uid | <code>string</code> | Unique identifier for the view instance. This can be used to generate unique IDs for elements within the view. It is automatically generated and should not be set manually. |

**Example**  
```js
import { View, Model } from 'rasti';

class Timer extends View {
    constructor(options) {
        super(options);
        // Create model to store internal state. Set `seconds` attribute to 0.
        this.model = new Model({ seconds : 0 });
        // Listen to changes in model `seconds` attribute and re-render.
        this.model.on('change:seconds', this.render.bind(this));
        // Increment model `seconds` attribute every 1000 milliseconds.
        this.interval = setInterval(() => this.model.seconds++, 1000);
    }

    template(model) {
        return `Seconds: <span>${View.sanitize(model.seconds)}</span>`;
    }

    render() {
        if (this.template) {
            this.el.innerHTML = this.template(this.model);
        }
        return this;
    }
}
// Render view and append view's element into the body.
document.body.appendChild(new Timer().render().el);
```

* [View](#module_view) ⇐ <code>Emitter</code>
    * _instance_
        * [.preinitialize(options)](#module_view__preinitialize)
        * [.$(selector)](#module_view__$) ⇒ <code>node</code>
        * [.$$(selector)](#module_view__$$) ⇒ <code>Array.&lt;node&gt;</code>
        * [.destroy(options)](#module_view__destroy) ⇒ <code>View</code>
        * [.onDestroy(options)](#module_view__ondestroy)
        * [.addChild(child)](#module_view__addchild) ⇒ <code>View</code>
        * [.destroyChildren()](#module_view__destroychildren)
        * [.ensureUid()](#module_view__ensureuid)
        * [.ensureElement()](#module_view__ensureelement)
        * [.createElement(tag, attributes)](#module_view__createelement) ⇒ <code>node</code>
        * [.removeElement()](#module_view__removeelement) ⇒ <code>View</code>
        * [.delegateEvents([events])](#module_view__delegateevents) ⇒ <code>View</code>
        * [.undelegateEvents()](#module_view__undelegateevents) ⇒ <code>View</code>
        * [.render()](#module_view__render) ⇒ <code>View</code>
    * _static_
        * [.uid](#module_view_uid) : <code>number</code>
        * [.sanitize(value)](#module_view_sanitize) ⇒ <code>string</code>
        * [.resetUid()](#module_view_resetuid)

<a name="module_view__preinitialize" id="module_view__preinitialize" class="anchor"></a>
### view.preinitialize(options)
If you define a preinitialize method, it will be invoked when the view is first created, before any instantiation logic is run.

**Kind**: instance method of [<code>View</code>](#module_view)  

| Param | Type | Description |
| --- | --- | --- |
| options | <code>object</code> | The view options. |

<a name="module_view__$" id="module_view__$" class="anchor"></a>
### view.$(selector) ⇒ <code>node</code>
Returns the first element that matches the selector, 
scoped to DOM elements within the current view's root element (`this.el`).

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>node</code> - Element matching selector within the view's root element (`this.el`).  

| Param | Type | Description |
| --- | --- | --- |
| selector | <code>string</code> | CSS selector. |

<a name="module_view__$$" id="module_view__$$" class="anchor"></a>
### view.$$(selector) ⇒ <code>Array.&lt;node&gt;</code>
Returns a list of elements that match the selector, 
scoped to DOM elements within the current view's root element (`this.el`).

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>Array.&lt;node&gt;</code> - List of elements matching selector within the view's root element (`this.el`).  

| Param | Type | Description |
| --- | --- | --- |
| selector | <code>string</code> | CSS selector. |

<a name="module_view__destroy" id="module_view__destroy" class="anchor"></a>
### view.destroy(options) ⇒ <code>View</code>
Destroy the view.
Destroy children views if any, undelegate events, stop listening to events, call `onDestroy` lifecycle method.

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>View</code> - Return `this` for chaining.  

| Param | Type | Description |
| --- | --- | --- |
| options | <code>object</code> | Options object or any arguments passed to `destroy` method will be passed to `onDestroy` method. |

<a name="module_view__ondestroy" id="module_view__ondestroy" class="anchor"></a>
### view.onDestroy(options)
`onDestroy` lifecycle method is called after the view is destroyed.
Override with your code. Useful to stop listening to model's events.

**Kind**: instance method of [<code>View</code>](#module_view)  

| Param | Type | Description |
| --- | --- | --- |
| options | <code>object</code> | Options object or any arguments passed to `destroy` method. |

<a name="module_view__addchild" id="module_view__addchild" class="anchor"></a>
### view.addChild(child) ⇒ <code>View</code>
Add a view as a child.
Children views are stored at `this.children`, and destroyed when the parent is destroyed.
Sets the child's `parent` to this view.
Returns the child for chaining.

**Kind**: instance method of [<code>View</code>](#module_view)  

| Param | Type |
| --- | --- |
| child | <code>View</code> | 

<a name="module_view__destroychildren" id="module_view__destroychildren" class="anchor"></a>
### view.destroyChildren()
Call destroy method on children views.

**Kind**: instance method of [<code>View</code>](#module_view)  
<a name="module_view__ensureuid" id="module_view__ensureuid" class="anchor"></a>
### view.ensureUid()
Ensure that the view has a unique id at `this.uid`.

**Kind**: instance method of [<code>View</code>](#module_view)  
<a name="module_view__ensureelement" id="module_view__ensureelement" class="anchor"></a>
### view.ensureElement()
Ensure that the view has a root element at `this.el`.
You shouldn't call this method directly. It's called from the constructor.
You may override it if you want to use a different logic or to 
postpone element creation.

**Kind**: instance method of [<code>View</code>](#module_view)  
<a name="module_view__createelement" id="module_view__createelement" class="anchor"></a>
### view.createElement(tag, attributes) ⇒ <code>node</code>
Create an element.
Called from the constructor if `this.el` is undefined, to ensure
the view has a root element.

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>node</code> - The created element.  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| tag | <code>string</code> | <code>&quot;div&quot;</code> | Tag for the element. Default to `div` |
| attributes | <code>object</code> |  | Attributes for the element. |

<a name="module_view__removeelement" id="module_view__removeelement" class="anchor"></a>
### view.removeElement() ⇒ <code>View</code>
Remove `this.el` from the DOM.

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>View</code> - Return `this` for chaining.  
<a name="module_view__delegateevents" id="module_view__delegateevents" class="anchor"></a>
### view.delegateEvents([events]) ⇒ <code>View</code>
Provide declarative listeners for DOM events within a view. If an events object is not provided, 
it defaults to using `this.events`. If `this.events` is a function, it will be called to get the events object.

The events object should follow the format `{'event selector': 'listener'}`:
- `event`: The type of event (e.g., 'click').
- `selector`: A CSS selector to match the event target. If omitted, the event is bound to the root element.
- `listener`: A function or a string representing a method name on the view. The method will be called with `this` bound to the view instance.

By default, `delegateEvents` is called within the View's constructor. If you have a simple events object, 
all of your DOM events will be connected automatically, and you will not need to call this function manually.

All attached listeners are bound to the view, ensuring that `this` refers to the view object when the listeners are invoked.
When `delegateEvents` is called again, possibly with a different events object, all previous listeners are removed and delegated afresh.

**Listener signature:** `(event, view, matched)`
- `event`:   The native DOM event object.
- `view`:    The current view instance (`this`).
- `matched`: The element that satisfies the selector. If no selector is provided, it will be the view's root element (`this.el`).

If more than one ancestor between `event.target` and the view's root element matches the selector, the listener will be
invoked **once for each matched element** (from inner to outer).

The ancestors are read when the event reaches the root element, before any listener runs. A listener that re-renders
or removes nodes between the target and the root does not stop the others: the nodes it took out of the view are
skipped, and the walk goes on through the ones left, up to the root.

An event that does not bubble (`focus`, `blur`, `mouseenter`, `load`, `scroll`...) is matched against its target
alone, as it would be by a listener on that element. It is caught on its way down to the target, so its listener runs
before any listener on the target itself, and a `stopPropagation()` in it keeps the event from them. When views
nested in each other both match the target, the outer one runs first.

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>View</code> - Returns `this` for chaining.  

| Param | Type | Description |
| --- | --- | --- |
| [events] | <code>object</code> | Object in the format `{'event selector' : 'listener'}`. Used to bind delegated event listeners to the root element. |

**Example**  
```js
// Using prototype (recommended for static events)
class Modal extends View {
    onClickOk(event, view, matched) {
        // matched === the button.ok element that was clicked
        this.close();
    }
    
    onClickCancel() {
        this.destroy();
    }
}
Modal.prototype.events = {
    'click button.ok': 'onClickOk',
    'click button.cancel': 'onClickCancel',
    'submit form': 'onSubmit'
};

// Using a function for dynamic events
class DynamicView extends View {
    events() {
        return {
            [`click .${this.model.buttonClass}`]: 'onButtonClick',
            'click': 'onRootClick'
        };
    }
}
```
<a name="module_view__undelegateevents" id="module_view__undelegateevents" class="anchor"></a>
### view.undelegateEvents() ⇒ <code>View</code>
Removes all of the view's delegated events. 
Useful if you want to disable or remove a view from the DOM temporarily. 
Called automatically when the view is destroyed and when `delegateEvents` is called again.

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>View</code> - Return `this` for chaining.  
<a name="module_view__render" id="module_view__render" class="anchor"></a>
### view.render() ⇒ <code>View</code>
`render` is the core function that your view should override, in order to populate its element (`this.el`), with the appropriate HTML. The convention is for `render` to always return `this`.  
Views are low-level building blocks for creating user interfaces. For most use cases, we recommend using [Component](#module_component) instead, which provides a more declarative template syntax, automatic DOM updates, and a more efficient render pipeline.  
If you add any child views, you should call `this.destroyChildren` before re-rendering.

**Kind**: instance method of [<code>View</code>](#module_view)  
**Returns**: <code>View</code> - Returns `this` for chaining.  
**Example**  
```js
class UserView extends View {
    render() {
        if (this.template) {
            const model = this.model;
            // Sanitize model attributes to prevent XSS attacks.
            const safeData = {
                name : View.sanitize(model.name),
                email : View.sanitize(model.email),
                bio : View.sanitize(model.bio)
            };
            this.el.innerHTML = this.template(safeData);
        }
        return this;
    }
}
```
<a name="module_view_uid" id="module_view_uid" class="anchor"></a>
### View.uid : <code>number</code>
Counter for generating unique IDs for view instances.  
This is primarily used to assign unique identifiers to each view instance (`this.uid`), which can be helpful for tasks like 
generating element IDs.  
[Component](#module_component)s use `this.uid` to generate data attributes for their elements, to be looked up on hydration.  
For server-side rendering, this counter should be reset to `0` on every request to ensure that the generated 
unique IDs match those on the client, enabling seamless hydration of components.

**Kind**: static property of [<code>View</code>](#module_view)  
**Default**: <code>0</code>  
<a name="module_view_sanitize" id="module_view_sanitize" class="anchor"></a>
### View.sanitize(value) ⇒ <code>string</code>
Escape HTML entities in a string.
Use this method to sanitize user-generated content before inserting it into the DOM.
Override this method to provide a custom escape function.
This method is inherited by [Component](#module_component) and used to escape template interpolations.

**Kind**: static method of [<code>View</code>](#module_view)  
**Returns**: <code>string</code> - Escaped string.  

| Param | Type | Description |
| --- | --- | --- |
| value | <code>string</code> | String to escape. |

<a name="module_view_resetuid" id="module_view_resetuid" class="anchor"></a>
### View.resetUid()
Reset the unique ID counter to 0.
This is useful for server-side rendering scenarios where you want to ensure that
the generated unique IDs match those on the client, enabling seamless hydration of components.
This method is inherited by [Component](#module_component).

**Kind**: static method of [<code>View</code>](#module_view)  
<a name="componentpartial" id="componentpartial" class="anchor"></a>
## ComponentPartial : <code>object</code>
An opaque partial template produced by [component.partial](#module_component__partial).
It carries a template's structure and current expressions so the render engine can
patch it in place.

**Kind**: global typedef  
