spine/spine — pull request #491 by Termina1 (2013-07-26 10:18:32+00:00)
Title: Controller bindings for model proposal

This is an implementation for automatic synchronisation of elements owned by controller and given model.

```coffee
class TestController extends Spine.Controller
@extend Spine.Bindings

modelVar: 'tmodel'
bindings:
'.testValue': 'field'

constructor: ->
super
do @applyBindings
```

It is simple example of how it should work. We define the variable that should contain our model and then declare one binding. We want some element with class ```testValue``` to be in sync with model field ```value```.

The value synchronisation on the model side depends on event ```change```, so it does on the element side. The default way to extract value from an element is to call jquery ```element.val()```, however there is a specific implementation for checkboxes.

Moreover, you have full control over extracting and setting element value.

```coffee
TestController extends Spine.Controller
@extend Spine.Bindings

modelVar: 'tmodel'
bindings:
'.testValue':
field: 'value'
setter: (element, value) -> element.val(value)
getter: (element) -> element.val()
```

In this example instead of just passing model field name we pass object where: ```field``` stand for model field, ```setter``` is a function that invokes when model changed and we want to set a new value for an element. This function has two arguments: ```element``` and ```value```. Also, there is function ```getter``` which invokes when element changed and we want to extract its value.

In addition, ```direction``` parameter can be passed. It can have two values: ```model``` and ```element```, first means that we want to change value of model only, second is that we want to change only element value.

The only thing I can't figure out how to deal with. You need to call method ```applyBindings``` somewhere in controller constructor, because I didn't find out how to elegantly inject initialisation code in current implementation of controllers.