I am using knockout to get and receive an object via JSON. I have got a form that is used to display and edit values that are bound to a Javascript class. My form has got an option for saving and discarding my values. The problem is that if I enter a value to an observed property and click discard the values are not transmitted to the webservice but are saved in my "display form", though, which I do not want. If I click on discard the original values shall be displayed again. Is it possible to perform my value-updating on a button click event (e.g. clicking "save") ? Here a snippet of my current code:
jQuery(document).ready(function(){
ko.applyBindings(new MyModel());
});
function MyModel(){
this.Name = ko.observable("Bob");
}
<div data-bind="text: Name"></div>
<input type="text" data-bind="value: Name" />
<input type="button" onclick="discardChanges()" value="Discard" />
<input type="button" onclick="saveChanges()" value="Save" />
If I modified the name and click Discard the original value (in this case Bob) should be restored whereas when I click Save the new value should be assigned (e.g. this.Name("Bill")).
Does anyone have an idea how to solve my problem?