-
-
Save SabbeRubbish/6897914 to your computer and use it in GitHub Desktop.
The previous version of this Gist wasn't up to date anymore with the latest version of autoNumeric (1.9.17).
Not saying this works perfectly, but first tests show that it does work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.bindingHandlers.autoNumeric = { | |
init: function (el, valueAccessor, bindingsAccessor, viewModel) { | |
var $el = $(el), | |
bindings = bindingsAccessor(), | |
settings = bindings.settings, | |
value = valueAccessor(); | |
$el.autoNumeric(settings); | |
$el.autoNumeric('set', parseFloat(ko.utils.unwrapObservable(value()), 10)); | |
$el.change(function() { | |
value(parseFloat($el.autoNumeric('get'), 10)); | |
}); | |
}, | |
update: function (el, valueAccessor, bindingsAccessor, viewModel) { | |
var $el = $(el), | |
newValue = parseFloat(ko.utils.unwrapObservable(valueAccessor()), 10), | |
elementValue = parseFloat($el.autoNumeric('get'), 10), | |
valueHasChanged = (newValue != elementValue); | |
if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0")) { | |
valueHasChanged = true; | |
} | |
if (valueHasChanged) { | |
$el.autoNumeric('set', newValue); | |
setTimeout(function () { $el.change() }, 0); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage (often forgotten, isn't it?):
This is just a simple rewrite of the code. I don't like the fact that 2 bindings are needed (one for data and one for settings). Ideally you would combine it to something like: