Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ModelBinder<VM>

The modelbinder serves two purposes: First, the bi-directional data binding. Second, the converting of validation decorators into validation instructions.

The bi-directional binding is uni-directional if the element doesn't support user input. That means, you can bind any element. The binding is based on a {@link Proxy} class and requires ES2015 natively.

The binding two a model requires the decorator {@link Viewmodel}. Here you define the type for binding.

@ViewModel(UserModel)
export class Component extends BaseComponent<{}> implements IModel<UserModel>

At any part of your component you can now access a real object that is being synhcronized:

model: ModelBinder<UserModel>;

In the form you define the binding with the property n-bind like this:

<input type="text" n-bind="value: Name" ... />

The property has two parts:

  1. The HTML property the value binds to (in the example: value).
  2. The model property which is connected (in the example: Name).

The @nyaf/forms library provides few binder functions, that consists of a name (the property name) and a binder logic. For example, the * ValueBindingHandler has the name "value" and reads a value from {@link HTMLInputElement} 's value property. It also contains an event listener, that supervises the input event. Binders for text may not have an event listener and hence bind just uni-directional.

If you need specific binders for custom elements, just create one by implementing the IBindingHandler interface.

class MyHandler implements IBindingHandler {
  // your code here
}

This definition must be added to the local modelbinder instance. The decorator {@link @ViewModel} creates a property model, that provides an instance of {@link ModelBinder,} initialized and bound to the current component. In the component, it's recommended to do this in the Lifecycle.Load step, you can add your custom handler:

@ViewModel(UserModel, { handlers: { 'custom': new CustomHandler() } })

The model property is enforced by the {@link IModel} interface. That's for typing, the actual value is created at runtime from the decorator code.

In the form you can now bind to your custom property of your custom web component.

<app-myinput n-bind="custom: Name" ... />

Type parameters

  • VM: object

Hierarchy

  • ModelBinder

Index

Properties

_scopeProxy

_scopeProxy: ProxyConstructor

handlers

handlers: {}

Type declaration

state

state: ModelState<VM> = new ModelState()

Static _instanceStore

_instanceStore: Map<HTMLElement, ModelBinder<{}>> = new Map<HTMLElement, ModelBinder<{}>>()

Accessors

scope

  • get scope(): VM
  • set scope(scope: VM): void

Methods

subscribe

  • subscribe<T>(key: keyof T, cb: (key: string) => void): void
  • Use this to subsribe to model changes. If it's bound to an input element this will fire for all assigned bindings, which more convenient than listing for events directly. I's safe to assign the subscriber in the contructor of a component.

    Type parameters

    • T

    Parameters

    • key: keyof T

      The name of the property; must be a member of the view model.

    • cb: (key: string) => void

      A callback being executed when the value changes.

        • (key: string): void
        • Parameters

          • key: string

          Returns void

    Returns void

Static getInstance

Static initialize

  • Initialize a binder for the current form. This is global and you can bind only one form at a time. Add custom binders to bind non-trivial properties. Usually, you shoudn't call this directly. It's public because it's being called from the @ViewModel decorator. The decorators adds a hidden property to store the binder instance directly in the component.

    Parameters

    • component: BaseComponent

      The web component this binder is currently attached to.

    • Optional handler: {}

      Handler identifier, used in forms in n-bind="prop: Value".

    Returns ModelBinder<any>

Static initupdates

Generated using TypeDoc