DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

5

Basic MVVM Architecture in JavaScript with knockoutjs

โž– View
The view is a Graphic User Interface created using a markup language to represent data.

View binds to properties of a ViewModel through the data-bind concept, which indirectly connects to the model data.

View need not be changed for any alteration done in ViewModel.

Changes made to data in ViewModel are automatically propagated in View due to binding.

โž– Model
A model is domain data or a business object that holds real-time data.

The model does not carry behaviors.

Behavior is mainly implemented in business logic.

โž– ViewModel
ViewModel is the center place where data from Model and View's display logic are bundled together.

ViewModel holds the dynamic state of data.

There is an implicit binder between View and ViewModel to communicate with each other.

This binding is inclusive of declarative data and command binding.

Synchronization of View and ViewModel is achieved through this binding.

Any change made in View is reflected in ViewModel, and similarly any change in ViewModel gets automatically reflected in View.

The existence of this 2-way binding mechanism is a key aspect of this MVVM pattern.

const ViewModel = function (first, last) {
  this.firstName = ko.observable(first);
  this.lastName = ko.observable(last);

  this.fullName = ko.computed(function () {
    return this.firstName() + ' ' + this.lastName();
  }, this);
};

ko.applyBindings(new ViewModel('Planet', 'Earth'));
Enter fullscreen mode Exit fullscreen mode

A complete example here: https://stackblitz.com/edit/stackblitz-starters-ntxh9w?file=script.js


I hope you found it useful. Thanks for reading. ๐Ÿ™

Let's get connected! You can find me on:

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

MongoDB Atlas lets you build and run modern apps in 125+ regions across AWS, Azure, and Google Cloud. Multi-cloud clusters distribute data seamlessly and auto-failover between providers for high availability and flexibility. Start free!

Learn More

Top comments (3)

Collapse
 
fyodorio profile image
Fyodor โ€ข

Just curious, why knockout? Itโ€™s kinda deadโ€ฆ

Collapse
 
nhannguyenuri profile image
Nhan Nguyen โ€ข

I just use it to make a demo. We can use another ๐Ÿ˜

Collapse
 
artydev profile image
artydev โ€ข

knockout "dead" but still relevant :-)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM appsโ€”no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

๐Ÿ‘‹ Kindness is contagious

Explore this practical breakdown on DEVโ€™s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple โ€œthank youโ€ or question in the comments goes a long way in supporting authorsโ€”your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay