Skip to content

Instantly share code, notes, and snippets.

@jmarmolejos
Created June 23, 2015 14:06
Show Gist options
  • Save jmarmolejos/c1f6e23dc2b0dfa7bf6e to your computer and use it in GitHub Desktop.
Save jmarmolejos/c1f6e23dc2b0dfa7bf6e to your computer and use it in GitHub Desktop.
ko.numericObservable = function(initialValue) {
var _actual = ko.observable(initialValue);
var result = ko.computed({
read: function() {
return _actual();
},
write: function(newValue) {
var parsedValue = parseFloat(newValue);
_actual(isNaN(parsedValue) ? newValue : parsedValue);
}
});
return result;
};
var vm = {
firstName: ko.observable("Jose"),
age: ko.numericObservable(33),
clickHandler: function() {
console.log(typeof this.age());
}
};
ko.applyBindings(vm);
console.log(typeof vm.age());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment