mbest/knockout — Issue #11: there should be a way to create dependencies between bindings
Author: mbest
Created: 2012-02-10 00:40:10+00:00
State: open

If bindings are independent (#1), there probably needs to be a way to make some bindings dependent on others. This could either be set up in the binding itself: binding **a** is always dependent on binding **b**. Another way could be to specify the dependency in the binding string: `data-bind="a: foo, b: bar, dependencies: {a: 'b'}"` Maybe both could be used. Also you could specify multiple dependencies with an array: `dependencies: {a: ['b','c']}`. If binding **b** wasn't included, though, would we throw an error or ignore the dependency?

So what does it mean to have a dependency? I think it means two things:

1) The dependent binding will always be run after its dependencies. So in the example above, even though *a* is listed before *b* in the binding string, *b* will run first because *a* is dependent on it. This could cause a problem if the order of the bindings cannot be changed. For example, if *a* sets the contents of the element and *b* controls the binding of the contents, then *b* can't be run before *a*. The code would should probably catch this condition and throw an error.

2) The dependent binding will be updated whenever its dependencies are updated. A binding's `update` function is run whenever its dependencies are updated anyway. This would be a simple extension to have a binding also be dependent on another binding. But some bindings couldn't act as dependencies either because they don't use an `update` function, or because they handle updates internally (using `ko.computed` or `subscribe`).