I need to trim all my observables. How to do that without the html binding? I've tried Automatically trim whitespace from all observable values but it doesnt work for me. I use knockout version 3.0.0 and i've used both latest chrome and latest ie.
<div class="editor-label">
<b>Name: </b>
</div>
<div class="editor-field">
<input data-bind='value: name, valueUpdate: "afterkeydown"' placeholder="Enter name" /><br />
<span style="font-weight:bold" data-bind="text: name"></span>
</div><br />
<div class="editor-label">
<b>Age: </b>
</div>
<div class="editor-field">
<input data-bind='value: age, valueUpdate: "afterkeydown"' placeholder="Enter age" /><br />
<span style="font-weight:bold" data-bind="text: age"></span>
</div>
ko.subscribable.fn.trimmed = function () {
return ko.computed({
read: function () {
return this().trim();
},
write: function (value) {
this(value.trim());
this.valueHasMutated();
},
owner: this
});
};
if (!String.prototype.trim) {
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); };
}
var ViewModel = function () {
var self = this;
self.name = ko.observable().extend({ required: true }).trimmed();
self.age = ko.observable().extend({ required: true }).trimmed();
};
ko.applyBindings(new ViewModel());
Exception: Object doesn't support property or method 'trimmed'