During the past working days I have been a little confused about some concepts in the KnockoutJS and how stuff works in the hierarchy we are using, how should we bind stuff.
- First of all to get a brief introduction on KnockoutJS, it is a java script library written to apply MVVM pattern on html or ASP MVC ”if you do .NET ” and by MVVM we can only say the word that we used very often while doing Silverlight or WPF, the word is BINDING the magical word that simply makes the speed and interactivity between your model and view irresistible, do we use binding yes !!! and boy this is one sexy word that I would love to use, binding was one of the strongest points that Silverlight had, thanks to knock out we can do it here
- you can take a quick overview from the magnificent Steve Sanderson “the creator of the library” he has made an appearance in MIX 11 and he sure made the impression check out the KnockoutJS site from here http://knockoutjs.com/ and here is a the video link available also at site http://channel9.msdn.com/Events/MIX/MIX11/FRM08
- I am not going to dig deep into this as I am still new at it beside I don’t wanna do code, just concepts and clarifications to make life easy for those who want to use KnockoutJS.
- Why use it where should the model be how to bind all these questions I will answer with code sample to clarify if you have not seen Steve Sanderson video its ok, but I highly recommend, no body explains it better.
- First the create in the JS File to be the ViewModel : they act as in MVVM view Model how? Simply by including the binding data in Javascript objects or functions, in other words it is our DataContext which we should bind our views to and could contain eather java script dom objects or functions to be used.Here is an example of a model that use in a project of mine
- Note the ko.observable is a defined type in the knockout which has a built in notifier and thus notifies the view when it changes so it can be updated via binding
- Second how should we use the ViewMoel we created using JS: On the server side we pass our dynamic objects to the ViewBag “that’s it for you server-side nothing more”, next using javascript we fill our previously created objects in the ViewModel “which should be referenced and thus seen in the view” with the data from the ViewBag we can then add java script to perform a certain functionality of what so ever.
- this the part where the page loads and the data is filled from the server to the java script object created above, weather you are using MVC or ruby on rails or whatever you should be able to figure this step on your own
Comments
Post a Comment