Simplest MVVM Implementation Using Observable Design Pattern
17 May 2012
Today, I’ll write how to implement a simple MVVM implementation using observables.
The best-known MVVM libraries are Angular.js and Knockout.js. This example looks a little Knockout.js. But mooooore simpler; just to show you
how it works, probably you won’t use it on your projects.
Lets check how to do it!
It has only one function to observe: value, so we can define a variable like saying var test = MVVM.value(‘test’); and it get one parameter for default value.
At the end of declerations, it must be started saying MVVM.apply(), as you will see, it just fires the change:model event to rebind all views.
P.S: I used native JavaScript to manage DOM. You can change it into jQuery.
The Implementation
Lets create an HTML with data-bind attributes. If we call data-bind="innerHTML: test", it’ll run like element.innerHTML = this.test