Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching or sending data from a form using knockout.js

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    
})
like image 687
simple Avatar asked Apr 26 '13 22:04

simple


People also ask

How do I get Knockout JS data?

prototype. loadNote = function (id) { var self = this; $. getJSON(uri + '/' + id, function (data) { self. note(data); }); }; // Apply bindings ko.

What is knockout js used for?

Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model.

Do people still use knockout JS?

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.

What is two-way data binding in knockout JS?

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.


1 Answers

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

like image 182
Ilya Avatar answered Oct 01 '22 20:10

Ilya