I'm using knockoutjs to handle different events - one of them is i want to add thousand seperators to a price input field.
So i subscribe a change event to my price field and that does some magic to add missing seperators and writes it back into the price field. The write back to the price field triggers a new change event and thus, i need a check to break the loop - which i do by asking if the value actually changed:
this.listingPriceFormatted.subscribe(function (newValue, model) {
var cleanValue = newValue.toString().replace(/\D/g, '');
$('#Price').val(cleanValue);
var outValue = MySite.Utilities.addThousandSeperator(cleanValue);
if (newValue != outValue) {
me.listingPriceFormatted(outValue);
}
});
I don't really like triggering the same event twice just to set the value once - is there any way i could write back to the field without triggering the event again, or am i doing this all wrong?