Introducing Knockout mapping
In this section, we will talk about the knockout.mapping
plugin. This plugin allows you to map some plain JavaScript object to a Knockout.js ViewModel with observable properties. It can be very useful when you want to receive some data from the server in the JSON format and show it to the user via declarative bindings.
A manual mapping example
For better understanding of the mapping concept, let's consider an example with manual mapping of JSON data to a ViewModel object. We will take a very abridged version of the library model from the previous chapters. Suppose the model contains only two properties: LibraryName
and AmountOfBooks
.
- Let's define a corresponding ViewModel:
var libraryViewModel = { LibraryName: ko.observable(), ...
Get Getting Started with Knockout.js for .NET Developers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.