Home

Awesome

DBJS-DOM

DOM bindings for DBJS objects

Installation

NPM

In your project path:

$ npm install medikoo/dbjs-dom

Browser

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Introduction

Bindings are split into two categories:

Text bindings

To add bindings for basic types of DBJS engine just do:

require('dbjs-dom/text')(db); // db is your DBJS database instance

After that each DBJS property observable will have implemented toDOM method, that will return DOM objects representing the value.

toDOM is environment agnostic, and needs document object to be passed as first of the arguments.

var p = document.body.appendChild(document.createElement('p'));
p.appendChild(user._firstName.toDOM(document));
user._firstName.toDOMAttr(p, 'title');

Above describes low-level way of working with things, which it's not very effective for common dev work. You can configure your tool of choice to detect toDOM functions automatically and call them behind the scenes, e.g. it's already supported by domjs, with which you an work as follows:

h1({ title: article._title }, article._title);
p(article._content);

Additionally to basic type bindings there's a special handling provided for DBJS-EXT's Enum type. All extensions handling need to be required individually:

require('dbjs-dom/text/enum')(EnumType);

Input bindings

Input binding are more complex than text bindings, as producing a form input control they resolve from all meta characteristics of a property.

To load bindings for basic types do

require('dbjs-dom/input')(db); // db is your DBJS database instance

Having that, you generate reactive input control for any mutable property:

p.appendChild(user._firstName.toDOMInput(document));

You can additionally customize the input by providing additional options:

user._firstName.toDOMInput(document, {
  required: true // force it to be required, even though it's not required in model
});

Possible Input configurations:

Options applicable to all kind of inputs
Options applicable to multiple inputs
Boolean type

By default represented with input[type=radio] controls

Additionally supported options:

Number type

By default represented with input[type=number]

Additionally supported options:

Percentage type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/number/percentage')(db);

By default represented with input[type=number]

Internally follows logic for Number but ensures that 1% is exposed as 1 and not 0.01 as it would happen using normal Number binding

Time type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/number/integer/u-integer/time')(db);

By default represented with input[type=time]

Internally extends DateTime DOM binding, so same options apply

String type

By default represented with textarea

Additionally supported options:

StringLine type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/string/string-line')(db);

By default represented with input[type=text]

Additionally supported options:

Email type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/string/string-line/email')(db);

By default represented with input[type=email]

Additionally supported options:

Password type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/string/string-line/password')(db);

By default represented with input[type=password]

Additionally supported options:

Url type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/string/string-line/url')(db);

By default represented with input[type=url]

Additionally supported options:

Enum type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/enum')(db);

By default represented with select

Additionally supported options:

DateTime type

By default represented with input[type=datetime-local]

Additionally supported options:

Date type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/date-time/date')(db);

By default represented with input[type=date]

Additionally supported options:

Object type

By default represented with select in which all type instances are listed.

Additionally supported options:

File type

Binding needs to be additionally loaded via:

require('dbjs-dom/input/object/file')(db);

By default represented with input[type=file]

Additionally supported options:

Composite inputs

Few inputs can be configured in related group of controls, e.g. we can configure dynamic property that relies on few static properties, and then express it with set of composite inputs.

Predefined composite inputs can be found at https://github.com/medikoo/dbjs-dom/blob/master/input/composites/

Input component

Input component is input control for one property but accompanied with label, hint, statuses (required, valid, saved), and place for error message. You can generate full input component with toDOMInputComponent method:

p.appendChild(user._firstName.toDOMInputComponent(document));

You can override output structure, with render option. See default render function, to see how to configure custom function.

Fieldset of controls

There's dedicated utility to output fieldset of controls:

require('dbjs-dom/input/utils/fieldset');

Then each object has toDOMFieldset method:

div(user.toDOMFieldset(document, {
	tag: 'personal' // Output only properties tagged as 'personal'
}));