Created
June 23, 2015 14:06
-
-
Save jmarmolejos/c1f6e23dc2b0dfa7bf6e to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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