Does knockout.js really use the MVVM pattern?

I am new to knockout.js. A few moments ago, I read the basic functions of co. I could not understand if this is really MVVVM? Because all they talk about is data binding and ease of use. But I'm sure MVVM is more than data binding, isn't it?

+4
source share
3 answers

Yes, knockout.js applies the MVVM pattern. This is explained in the documentation :

  • A model : your applications store data. This data represents objects and transactions in your business domain (for example, bank accounts that can carry out money transfers) and are independent of any user interface. When using KO, you usually make Ajax calls to some server code to read and write stored model data.

  • View Model: A clean view of data and operations in the user interface. For example, if you create a list editor, your view model will be an object containing a list of elements, and exposing methods for adding and removing elements.

    Note that this is not the user interface itself: it has no concept of buttons or display styles. This is not a permanent data model β€” it contains unsaved data that the user is working with. When using KO, your view models are pure JavaScript objects that do not speak HTML. Saving an abstract review model this way allows you to stay simple, so you can control more complex behaviors without getting lost.

  • A view : A visible interactive interface representing the state of the view model. It displays information from the view model, sends commands to the view model (for example, when the user clicks the buttons), and is updated whenever the state of the view model changes.

    When using KO, your presentation is just your HTML document with declarative bindings to link it to the view model. Alternatively, you can use templates that generate HTML using data from your view model.

+8
source

In addition to the answer already provided, there are a few things to keep in mind -

MVVM

The knockout is MVVM because it supports a good separation of concerns. Unlike other JavaScript libraries such as jQuery, the goal is not to pollute the view with something that does not concern it.

The purpose of the view model is important for understanding. It does not try to manipulate the DOM, because only the logic necessary to transfer data to the view is placed inside it.

The purpose of the view is only to present the data. There is no logic, verification, or other logic code.

model is the only place knockout can get a little confused. It’s usually good practice to place a separate model in your project to use Knockout, but many developers have found it easy to mix model into a view model . The reason for this is obvious (some model are very basic), but again this is only because of the ease of implementation.

MVC vs MV *

Of course, there are other answers on SO.com that try to answer that there is MV* , but I wanted to drop $ 0.02 here. Other libraries or frameworks say they are MVC or MVP or MV(whatever) , but Knockout is the only thing I have found that practices what it preaches in this regard. If you have the time and desire, look at the structure of other frameworks, such as Angular or Ember, and you will see that there is a blurry line that exists, and more or less they just use the MVVM pattern, but calling it something else.

+2
source

Well, I think it is possible, but I am working on a project in which all the layout of the layout and layout of the user interface is done in the js ViewModel new file, which is not good practice.

0
source

Source: https://habr.com/ru/post/1497556/


All Articles