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).
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.
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?
I always wanted to write an extender. So here comes another answer to your question that is implemented via a knockout extender.
I'm still undecided wether I like this one or the one with the Percentage class better.
<input data-bind="value: p1.percentage, valueUpdate: 'afterkeydown'"></input>%
= <span data-bind="text: p1"></span>
ko.extenders.percentage = function(target, option) {
target.percentage = ko.computed({
read: function() {
return this() * 100;
},
write: function(value) {
this(value/100);
},
owner: target
});
return target;
};
var model = {
p1: ko.observable(0.5).extend({'percentage': true})
}
ko.applyBindings(model)
http://jsfiddle.net/DWRLr/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With