I want to format numbers in a comma separated fashion. I have found this jsfiddle which caters to my requirement. I need to add another pureComputed
value for my vm, and I tried it with this way by modifing modified jsfiddle.
However, I'm getting an error in console saying:
Unable to process binding "text: function (){return Test }" Message: undefined is not a function
.
What am I missing here? my js is
(function ($) {
var cleanInput = function (value) {
return parseFloat(value.replace(/[^0-9.-]/, ''));
}
var formatInput = function (value) {
toks = value.toFixed(2).replace('-', '').split('.');
var display = $.map(toks[0].split('').reverse(), function (elm, i) {
return [(i % 3 == 0 && i > 0 ? ',' : ''), elm];
}).reverse().join('') + '.' + toks[1];
return value < 0 ? '(' + display + ')' : display;
}
ko.bindingHandlers.money = {
init: function (elm, valueAccessor) {
$(elm).change(function () {
valueAccessor()(cleanInput(elm.value));
}).addClass('money');
},
update: function (elm, valueAccessor, allBindingsAccessor) {
var value =ko.utils.unwrapObservable(valueAccessor())
$elm = $(elm),
method = $elm.is(":input") ? "val" : "html";
$elm[method](formatInput(value)).toggleClass('negative', value < 0);
}
};})(jQuery);
$(function(){
var viewModel={
Cash:ko.observable(1234.56),
Test : ko.pureComputed(function() {
return (self.Cash() * 10).toFixed(0);
})
}
ko.applyBindings(viewModel);
});