I have been trying to figure out the right approach to displaying/editing percentage values in Knockout-JS (and more generally how I should create reusable components like these).
My ViewModel has an observable value which is a percentage stored as a fractional number, e.g. 0.5 to represent 50%. I would like to display and edit the value in the percentage format (e.g. '50') so the users don't get confused (they get confused easily).
writeable computed
I was able to a simple version by setting up a writeable computed function: see http://jsfiddle.net/Quango/fvpjN/
However, this is not very re-usable, as it would need to be reimplemented for each value. I experimented with an extender but this effectively masked the underlying value and so made it unusable.
BindingHandlers
I think what I need is a binding handler, so instead of writing
<input data-bind="value: commission" />
I would write
<input data-bind="percentage: commission" />
I had a look at the code in the "value" bindingHandler in knockout.js, but there is a lot of code there for the binding and I don't want to replicate that.
So my questions are:
is there a good/standard/template way to do this sort of value conversion?
if not, is there a way to re-use the "value" binding without having to copy and paste the existing code?