Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
I was reading someone's code where I came across Knockout and MVVM. I did some reading on both topics, but I'm still confused as to what problems they really solve, most likely because I just haven't built applications large enough to come across the problems that this framework/architecture solves.
I spent some time to understand this sample code -- http://knockoutjs.com/img/homepage-example.png -- from the Knockout home page. I was hoping if someone could explain to me what the same code would look like if Knockout were not used, and how that could be problematic.
(SO may not be the right platform for this question, so please let me know if there's some other Exchange that's more appropriate).
Thanks!
From 10,000 Feet
Knockout provides two-way data-binding between a view written in HTML and corresponding properties and functions on a viewmodel written in JavaScript.
Imagine you have an HTML view called contacts.html and a JavaScript viewmodel called contacts.js. Those two together would make a module, and Knockout would be the glue that binds them together.
MVVM stands for Model View ViewModel. I addressed the latter two above. The model is simply a JavaScript representation of a particular corner of your world, say, in this case, a Contact.
So, bringing together the above, we might have (in terms of directory structure):
- models\contact.js
- views\contact.html
- viewmodels\contact.js
You might instantiate your model inside your viewmodel, and then bind your view to the viewmodel using Knockout.
MVVM simply provides for a great way to separate concerns and maximize reuse. As an example of reuse, you could bind your contacts view to many different contact viewmodels, depending on context. The context could be the size of the client device, a user's authorization profile, a "community" versus a "premium" version of your application, and so on.
Modifying by improvement any of the components of MVVM can be done in relative isolation without adversely affecting the application as whole. Hence the value of separating concerns.
Does that make sense?