When extending the functionality of Knockout, when is it appropriate to use custom bindings vs extenders vs custom functions using “fn”? Are there performance implications when choosing one over another?
When extending the functionality of Knockout, when is it appropriate to use custom bindings
Share
You would generally want to choose custom bindings when the functionality that you are trying to accomplish involves both the UI and your data, so where you need access to an element and usually some data to control what to do with the element.
There is really not a big difference between extenders and adding to
.fnat this point. Extenders were added to KO slightly before shared functions were available via.fn.It is usually not an issue, but extenders will just work for both observables and computed observables. Using
.fnyou would need to add functions toko.subscribable.fnfor it to be available to both. However, in some cases your functionality may only apply to one or the other, so usingko.observable.fnandko.computed.fnwould be a better choice.I find extending
.fnto be the cleanest syntax. If you are sure to returnthisor a new type if you are wrapping it, then you can support chaining easily. Extenders are a little more explicit and ensure that the API surface areas of the types remain lean (for what that is worth).