Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Knockout with custom bindings, extenders, or custom functions

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?

like image 616
Homer Avatar asked Dec 13 '22 03:12

Homer


1 Answers

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 .fn at 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 .fn you would need to add functions to ko.subscribable.fn for it to be available to both. However, in some cases your functionality may only apply to one or the other, so using ko.observable.fn and ko.computed.fn would be a better choice.

I find extending .fn to be the cleanest syntax. If you are sure to return this or 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).

like image 116
RP Niemeyer Avatar answered May 03 '23 23:05

RP Niemeyer