Repository navigation
Bindings and Cells
source = new nx.Cell
target = new nx.Cell
source['->'] target
source.value = 'cellar door'
# target.value is now 'cellar door' tooReverse 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['<-'] sourceUsually 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 = 1converter is called with 1 and undefined and returns 1
source.value = 2converter is called with 2 and 1 and returns 3 and so on.
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
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
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 hereYou can also supply a converter passing it as the second argument
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.nameThis binding will persist when selected_user_id is changed and a different user is selected for editing.
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'