Skip to content
Alexander Gerasimov edited this page Dec 10, 2015 · 2 revisions

Bindings

Simple cell binding

source = new nx.Cell
target = new nx.Cell
source['->'] target
source.value = 'cellar door'
# target.value is now 'cellar door' too

Reverse binding notation is an alternative form of writing a binding statement (works for all kinds of bindings):

source = new nx.Cell
target = new nx.Cell
target['<-'] source

Converters

Usually you want to convert cell values as they are passed from cell to cell by specifying a converter:

source = new nx.Cell
target = new nx.Cell
source['->'] target, (value) -> value.toUpperCase()
source.value = 'cellar door'
# target.value is now 'CELLAR DOOR'

In most cases converters are called with the newly set cell value and and the value the cell had previously:

source = new nx.Cell
target = new nx.Cell
source['->'] target, (value, old_value) ->
  if old_value?
    old_value + value
  else
    value

source.value = 1

converter is called with 1 and undefined and returns 1

source.value = 2

converter is called with 2 and 1 and returns 3 and so on.

Immediate sync

Binding and immediately updating the target cell is useful when you create bindings after your data flow has already been initialised. Use double arrows to bind-and-sync:

source = new nx.Cell value:'cellar door'
target = new nx.Cell
source['->>'] target
# target.value is now 'cellar door'

Note the source cell initialisation with the value option

Merging binding

Binds to a array of cells and passes all the values to the converter:

click = new nx.Cell # click events
button_enabled = new nx.Cell value:no

action['<-'] [click, button_enabled], (click, enabled) ->
  click if enabled

Demux binding

Pass a cell-picking function as the first argument:

users = new Map [...] # user-by-id map
message['->'] (message) -> 
  user = users.get message.recepient_id
  user.message # return a cell or an array of cells here

You can also supply a converter passing it as the second argument

Flattening binding

is useful when you need to keep a binding to cells that are themselves values of a cell. For instance, you want to edit user data for one of a collection of users.

user = 
  name: new nx.Cell

users = new Map [...] # user-by-id map

selected_user_id = new nx.Cell
selected_user = new nx.Cell
selected_user_id['->'] selected_user, (id) -> users.get id

# Form
form.name = new nx.Cell
# update user name with the one in the form
form.name['->*'] selected_user, (user) -> user.name

This binding will persist when selected_user_id is changed and a different user is selected for editing.

Binding shorthand in cell constructor options

The example from above rewritten with shorthand:

source = new nx.Cell
target = new nx.Cell
  '<-': [source, (value) -> value.toUpperCase()]
source.value = 'cellar door'
# target.value is now 'CELLAR DOOR'