mikeric/rivets — pull request #154 by mikeric (2013-04-27 03:01:25+00:00)
Title: View context updating

This pull-request adds a much needed API for updating models on a currently bound view. I've seen this being done a couple of ways, but most often I see this as an example.

```coffeescript
# bind a view with two model contexts.
view = rivets.bind el, item: item, user: john

# unbind all current bindings for the view.
view.unbind()

# change the models on the view.
view.models = {item: item, user: jane}

# rebuild the view so that we get new bindings
# that have references to the new models.
view.build()

# finally, bind the view's binding instances.
view.bind()
```
Aside from being verbose and order-sensitive (otherwise introducing memory leaks), it's also slow because we need to parse the DOM tree for that view as we're calling `view.build()`. Another issue is that we're doing unnecessary rebuilding of bindings for the `item` model, since that didn't change.

### View context updating

This pull request adds a new function to the view — `update` — which takes an object containing only the models that you wish to replace with another object.

```
view.update user: jane
```

And thats it, now only the bindings that we're bound to "user" will get updated and re-bound, all other bindings will be unaffected by the change. Bindings now hold reference to the actual key ("user" in this case), so we can simply grab those bindings and update their model to the new one, without needed to parse the DOM tree and rebuild any bindings.