The view-model
The view-model is a pure code representation of the data and operations on a UI. It isn't the UI itself. It doesn't have any concept of buttons or display styles. It's not the persisted data model either. It holds the unsaved data the user is working with. View-models are pure JavaScript objects that have no knowledge of HTML. Keeping the view-model abstract in this way lets it stay simple, so you can manage more sophisticated behaviors without getting lost.
To create a view-model, we just need to define a simple JavaScript object:
var vm = {};
Then to activate Knockout, we will call the following line:
ko.applyBindings(vm);
The first parameter says which view-model object we want to use with the view. Optionally, we can pass a second ...
Get KnockoutJS Essentials 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.