MVVM, short for Model-View-ViewModel, is an improvement on the MVC and MVP architectural patterns. In the MVVM pattern, the state and behavior of the View in the MVC pattern are abstracted, separating the development of the user interface (UI) from the development of business logic and behavior within the application more clearly.
The MVVM pattern simplifies the dependency between the interface and business logic, helping to separate the development of the graphical user interface from the development of business logic or data models. In MVVM, the ViewModel acts as a binder to link the UI layer to the data layer Model. When the Model updates, the ViewModel updates the data to the View through the binder. When the View triggers a command, it communicates the message to the Model through the ViewModel. The ViewModel acts as a black box, allowing developers to focus only on the presentation of the UI in the view layer and the data layer Model, without needing to be concerned with how the ViewModel passes data and messages.
The ViewModel is an abstraction of the view that exposes public properties and commands.
The binder in the ViewModel communicates between the view and data binders.
When the Model is updated, the ViewModel updates the data to the View through the binder. When the View triggers a command, it communicates the message to the Model through the ViewModel.
Loose coupling: The View can change and modify independently of the Model. A ViewModel can be bound to different View instances, whereby when the View changes, the Model can remain unchanged, and vice versa.
Reusability: It is possible to consolidate some view logic in a ViewModel, enabling multiple View instances to reuse this segment of view logic.
Independent development: Developers can focus on the development of business logic and data in Model, while designers can concentrate on page design.
Testable: User interface testing is usually challenging, but behavioral testing can be conducted through the ViewModel.
For overly large projects, data binding might require more memory consumption.
Data binding can make debugging more difficult. When there's an abnormality in the interface, it could be due to issues in the View code, or it could be due to problems in the Model code. Data binding can rapidly propagate a bug from one location to another, making it less straightforward to pinpoint the original problematic area.
Below is a simple data binding example implemented with reference to Vue. However, with regards to Vue, it's worth noting that the official documentation mentions that Vue does not fully adhere to the MVVM model, but its design is certainly inspired by it. For further information on why Evan You, the creator of Vue, states that Vue does not fully adhere to MVVM, one can refer to this link.
var vm =newMvvm({el:"#app",data:{msg:"1",date:newDate(),obj:{a:1,b:11}}})functionupdate(){ vm.msg ="updated";}///////////////////////////////////////////////////////////////////////////////</script></html>