O'Reilly logo

Mastering KnockoutJS by Timothy Moran

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Knockout Mapping

The Knockout Mapping plugin is the answer to projects that want to bind against their server's AJAX responses without manually writing the JavaScript classes in order to convert them into observables. The mapping plugin will convert JavaScript objects or JSON strings into objects with observable properties:

var mappedViewmodel = ko.mapping.fromJS({
   name: 'Timothy Moran',
   age: 24
});
ko.applyBindings(mappedViewmodel);

For JSON, take a look at the following code:

var serverResponse = "{"name":"Timothy Moran","age":24}";
var mappedViewmodel = ko.mapping.fromJSON(serverResponse);
ko.applyBindings(mappedViewmodel);

The mapping plugin handles arrays by converting them into observableArrays. It also creates a copy of objects, allowing a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required