twine

A minimalistic two-way binding system. View twine on GitHub.


Basic bindings

Bindings are defined with strings of JavaScript directly on a DOM node. This means you're already familiar with the binding syntax, and you have access to all the properties inside your binding definition that the underlying object does.

For input elements, the initial value of a binding will be read from the DOM if it's not already defined:

characters used.

Contexts

Every binding is evaluated relative to a context. All this means is that when you write data-bind="key", you're accessing the key property of the current context. Any value can be used as a context, so most of the time you'll use a plain ol' JavaScript object.

The context for this page is stored on window, so you can access it in the console if you want. Since it's just an object, it's easy to encode:


In this demonstration, the context was defined and initialized with:

var context = {};
Twine.reset(context).bind().refresh();

The context is shown in the sidebar for reference as you peruse.

Sub-contexts

It is also possible to create sub-contexts with the define attribute.

Hello,

data-define can be used for context creation or for introducing variables into the current context.

I'm visible!

This can be pretty useful for when you want to use data-bind-show and start in a visible state.

data-context lets you change the current context for every element nested inside.

Define-array

data-define-array lets you define an array of objects one at a time in the context and implicitly reference the correct member in the array.

5

data-define-array can also be used with data-context

Visibility bindings

data-bind-show adds the class hide to the node. You can then use CSS like .hide { display: none; } or similar to hide the node and its children from view.

That color is not longer than 4 characters.

Class bindings

data-bind-class toggles a node's classes, given an object with class names for keys and booleans for values.

Checkboxes

The binding value of checkboxes is a boolean indicating the checked state.

Radio buttons

The binding value of radio buttons is the value attribute of the currently selected button. Make sure to use the same key for every button in the radio group.

State:

Alternatively, you may use data-bind-checked, which is a one-way binding of the checked attribute.

Attribute bindings

The following DOM attributes can be bound to via:

Search Google for

Event bindings

DOM events are available for quick bindings. The default behaviour (think event.preventDefault) will be prevented, unless either:

Additionally, the presence of allow-default attribute will cause the twine system to never prevent default, rending the two exceptions above moot.

The following events are provided:

Example: click events

Remember how bindings are just JavaScript? The same applies for event handlers.

green or blue

However, inline mutation is pretty ghetto, so let's define some functions on our context. (these won't appear in the sidebar since JSON.stringify ignores functions)

Switch to

Event arguments

Events bound in this way have access to a few named parameters that you can pass in:

You pressed key:

inside of input with ID:

Further, as you can see from the example keyPressed above, this inside of a function called this way will refer to the current context.