I´m using that plugin : https://github.com/plentz/jquery-maskmoney to format my money editor...
I tried to use KnockoutJS in that editor, but it does not work... Without that mask all works fine...
My code test is simple :
<input id="Price" data-bind="value: Price" type="text" name="Price">
Javascript to Mask input
$("#Price").maskMoney({ symbol: 'R$ ', showSymbol: true, thousands: '.', decimal: ',', symbolStay: false });
And KnockoutJS
var ViewModel = function () {
this.Price = ko.observable();
this.PriceFinal= ko.computed(function () {
return this.Price()
}, this);
};
ko.applyBindings(new ViewModel());
You should use a writable computed observable.
You can also register a binding handler for MaskMoney with Knockout, something like:
});
and then as your binding:
Note that I tweaked the MaskMoney plugin slightly to use
input.on('focusout.maskMoney', blurEvent);
rather thaninput.bind('blur.maskMoney',blurEvent);
because it was not triggering an update on losing focus via mouse clicking, only on tabbing.I'm new to Knockout and have been finding the binding handler approach really nice for plugins like this and datepickers, etc.
If you are using jquery.formatcurrency you could do: