Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout JS - Why do all the examples only contain ViewModel/View but no Model

I am looking through alot of the Knockout JS documentation available, however it doesn't seem to follow the MVVM pattern as I would expect (when looking at Wikipedia's definition of MVVM).

In all the examples they seem to show source code for ViewModels and Views however there is never a model, now it seems like most of the functionality that the model should contain (saving/retrieving a representation of data) is put within the ViewModel. I thought that maybe the ViewModels were actually more akin to the Model, and the binding layer that you kinda get for free is the ViewModel, as that does all the bindings...

So I am just wondering if I am missing something here? As I have a question open at the moment about where my UI logic should go, i.e adding Watermarks, Inter-View chatter etc and I haven't really got a solid answer for it, so wanted to confirm that my understanding of the pattern usage within this framework is correct before continuing.

like image 414
somemvcperson Avatar asked Jun 29 '11 12:06

somemvcperson


People also ask

What is ViewModel in knockout JS?

The second is the ViewModel, which in this example is the JavaScript variable/function called viewModel that contains a single variable name. The third is telling Knockout to perform the data binding of the view and the ViewModel. This is accomplished by calling the ko. applyBindings function with a ViewModel.

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.

Is knockout js still used?

Today, Knockout. js mostly exists in legacy applications and developers try to move to something newer, like Vue. js.


1 Answers

In Web development, the Views and ViewModel are at the client-side.

The Models are at the server side.

The models represent the real objects while the View Models only represent them in terms of the view in which they are displayed e.g. in Customer Model you have all the information related to the Customer but in a Customer View Model you might only have Customer Name (because that's all your showing on the view).

Most of the knockoutjs example don't explain the Model bit is because it is server-side dependent and could be written in Ruby/C#/Python etc. Knockout only deals with the View Model and View; It's server side agnostic.

The server side tech is required to develop the Model layer in MVVM. Knockoutjs is required to develop the VM-V layer.

like image 179
neebz Avatar answered Nov 15 '22 07:11

neebz