Knockout: Provide a way to get all bindings in a binding handler

Created on 3 Aug 2016  路  4Comments  路  Source: knockout/knockout

I see in the source code that it's possible to call allBindings as a function and it will return an object with all binding keys and their appropriate value accessors. However I also see the note that this kind of usage is deprecated.

The current version exposes only a get and a has function, and they both operate with a concrete binding key as a parameter. So basically there is no chance to get all bindings except using the deprecated function call.

First I'd like to ask why it was deprecated. Was there any kind of technical issue, or performance problem, or anything? Or was it just an API change so that using it in most cases got nicer and simpler?

If there are no technical limitations, it would be good to either have an all function on allBindings, or modify get to be able to call it without parameter, and in that case it would return all bindings.

All 4 comments

The deprecated method returns an object that contains the values of all bindings. To access those values, it will possibly add dependencies to those binding within the calling code. Generally this is undesirable since only a specific binding value is needed.

Perhaps a new API could return a list of the binding names.

I understand that, but using the get function will add dependencies to the requested binding value too, won't it? Or I didn't understand you properly maybe. Do you mean using allBindings() in a computed will automatically add all bindings' values as dependencies?

Anyway, in my current use case it would be enough to get the names. I think in most cases it's enough, and then one could get the specific values using the get function.

The new function could be called keys or names (I personally prefer keys) for example, so we could use it like

...
var bindingNames = allBindings.keys();
...

@mbest do you mind merging this feature if I make a PR?

I like it.

Was this page helpful?
0 / 5 - 0 ratings