Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Model' and 'ViewModel' in Knockout.js

In MVC, 'Model' is just code representation of data (e.g. in ASP.NET MVC it's a class with according fields).

In Knockout however (which employs MVVM), I see that object with fields is called a 'ViewModel'. From official KO documentation:

A model: your application’s stored data. This data represents objects and operations in your business domain (e.g., bank accounts that can perform money transfers) and is independent of any UI. When using KO, you will usually make Ajax calls to some server-side code to read and write this stored model data.

A view model: a pure-code representation of the data and operations on a UI. For example, if you’re implementing a list editor, your view model would be an object holding a list of items, and exposing methods to add and remove items.

From examples, it can be seen that ViewModels are objects with fields, holding the data, what usually'd be done by Model in MVC:

var myViewModel = {
    personName: ko.observable('Bob'),
    personAge: ko.observable(123)
};

So I'm a little lost here. What exactly 'Model' and 'ViewModel' mean in Knockout.js domain?

like image 801
Arnthor Avatar asked Dec 16 '13 10:12

Arnthor


People also ask

What is the difference between View and ViewModel?

VIEW: ( Platform Specific Code – USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Model and exposes it to the View. This is the model specifically designed for the View.

What is MVVM in KnockoutJS?

Advertisements. Model-View-ViewModel (MVVM) is an architectural design pattern for developing software applications. MVVM was developed by Microsoft Architect John Gossman in 2005. This pattern is derived from Model-View-Controller (MVC) pattern.

How do we activate a Knockout model?

To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.

Can we have multiple Knockout models on a single page?

Knockout now supports multiple model binding. The ko. applyBindings() method takes an optional parameter - the element and its descendants to which the binding will be activated. This restricts the activation to the element with ID someElementId and its descendants.


2 Answers

In a JavaScript-implemented MVVM pattern, often the "Model" part is supplied by the web api. The data that is provided to the page is the model. Now whether that data is contained in an separate model object once it's in JavaScript - that's another story.

Often a View Model is just a Model that has been augmented with properties and functions to support the particular view that it is applied to. Client-side calculations, drop-down look-up values, client-side validation routines, etc. In this case, the view model might look like this:

var vm = {
    save: function(){ save logic... },
    cancel: function(){ cancel logic... },
    states: ko.observable(), //list of states for state dropdown
    customerId: ko.observable(),
    customerFirstName: ko.observable(),
    customerLastName: ko.observable()
};

Other times, the model will be maintained in a separate object. In that case the view model might look like this:

var customerModel = getCustomerFromDataSource();
var vm = {
    save: function(){ save logic... },
    cancel: function(){ cancel logic... },
    states: ko.observable(), //list of states for state dropdown
    customer: customerModel
};

The main thing to keep in mind is that the Model is the data and the View Model is the layer that makes your Model available to the view (usually via data-binding). Sometimes the Model may be a separate class; other times the Model is just a known set of properties in the View Model.

like image 95
Joseph Gabriel Avatar answered Oct 25 '22 22:10

Joseph Gabriel


Model
Models hold information, but typically don’t handle behaviour

View
View contains the data-bindings, events and behaviours which require an understanding of the Model and ViewModel. Although these behaviours can be mapped to properties, the View is still responsible for handling events to the ViewModel

ViewModel
ViewModel sits behind our UI layer. It exposes data needed by a View (from a Model) and can be viewed as the source our Views go to for both data and actions.

You can find out more information in the following link here
You can also find more information about mvc and mvvm in stackoverflow question

like image 23
Goddard Avatar answered Oct 26 '22 00:10

Goddard