Newbie in Knockout, also a front-end designer so I need plain talk.
I have a form that I need to send to the database and then retrieve from the database later.
Please explain in very simple terms how to produce a working example to illustrate saving and posting a form?
From knockout tutorial: http://knockoutjs.com/documentation/json-data.html I understand about getting and sending json data. How is the json data being matched to the form? What is mapping and is there a plugin or easy example of how to map the json data back to my form? Basically, how do I do what is commented inside the knockout code examples below?
Fetch Data:
$.getJSON("/some/url", function(data) {
// Now use this data to update your view models,
// and Knockout will update your UI automatically
})
Send Data:
var data = /* Your data in JSON format - see below */;
$.post("/some/url", data, function(returnedData) {
// This callback is executed if the post was successful
})
prototype. loadNote = function (id) { var self = this; $. getJSON(uri + '/' + id, function (data) { self. note(data); }); }; // Apply bindings ko.
Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model.
KnockoutJS is far from dead, and it's actually still being improved and evolved (see Technical Knockout) but newer frameworks seem a far better bet for our needs, considering activity and performance.
KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.
simple form
<form data-bind="submit: onSubmit">
<input data-bind="value : firstName"/>
<input data-bind="value : lastName"/>
<input type="submit" text="Submit!"/>
</form>
Response: <span data-bind="text : response"></span>
simple view model
var viewModel = new function()
{
var self = this;
self.firstName = ko.observable("default first");
self.lastName = ko.observable("default last");
self.responseJSON = ko.observable(null);
self.onSubmit = function()
{
var data = JSON.stringify(
{
first : self.firstName(), last : self.lastName()
}); // prepare request data
$.post("/echo/json", data, function(response) // sends 'post' request
{
// on success callback
self.responseJSON(response);
})
}
}
ko.applyBindings(viewModel);
JSFiddle DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With