<input type="text" data-bind="value: name" />
when the "escape key" is pressed while typing a value for [name], it should return to its previous value.
value remains the same even when I press the "escape key"
jsfiddle here
Your are expecting ctrl-z
key behavior attached to escape
key as escape key not built to undo your changes on press .
well to achieve this we can use keydown
event as keypress
wont detect escape key .
view:
<input id="test" type="text" data-bind="value: name,event:{keydown :escKey}" />
<pre data-bind="text:ko.toJSON($data,null,2)"></pre>
viewModel:
var model = function(){
var self=this;
self.name= ko.observable("smith");
self.escKey= function (data, event) {
if (event && event.keyCode == 27) {
$('#test').val(data.name());
}
return true;
}
};
ko.applyBindings(new model());
working sample here
Alternatively you can use biningHandlers
to achieve similar functionality but keep it as last resort .