Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a reasonable way to use MVVM coding patterns with HTML5

I have a nice application written in Silverlight. To allow greater cross browser support, we are considering rewriting with a HTML5 front end.

What would be a reasonable way to move from Silverlight using XAML and C# with MVVM coding patterns into a HTML5 front end.

On the other hand, if this is not a good idea and the MVVM pattern should be abandoned, please explain why.

like image 477
Noah Avatar asked Oct 19 '11 16:10

Noah


4 Answers

Knockout is a great javascript library which helps you to write MVVM client side. You create your view models in javascript and bind to them using html templates (with the help of jquery templating). Your view models can use ajax to retrieve data.

It is essentially the same as Silverlight development- you have HTML instead of XAML and javascript instead of C#/VB.Net. You could even use the same web services really...which might be helpful if you plan to offer both interfaces or conversion later on.

Edit
Thought that I'd add that IMHO MVVM should not be abandoned as it really does a great job of separating concerns (I guess when implemented correctly) but a big win surely is that it has the developer thinking about or in the mindset or separating responsibility in an application. I've used Knockout.js for web projects and Caliburn.Micro for Silverlight and have really enjoyed that style of development.

like image 101
davidsleeps Avatar answered Nov 04 '22 10:11

davidsleeps


Knockout is MVVM pattern implemented in HTML/Javascript with databinding and all. http://knockoutjs.com/

If what you want is achievable simply through DOM manipulation, and you're used to and like the MVVM pattern, Knockout should be a good choice.

like image 29
Davy8 Avatar answered Nov 04 '22 09:11

Davy8


Yeah, the MVVM pattern for HTML 5 is called MVC. Most easily implementable with MVC3. Provides a layer of abstraction in respect to the JavaScript code, support for ViewModels (aka Views) and separation of concerns between the views, data access, and business logic.

Best of all is that validation and UI event handling is wired for you in a simple manner, allowing you to abstract yourself from the intricacies of JavaScript event handlers for various HTML controls.

However if you strictly want to stick with HTML & JavaScript, I suggest rolling your own JavaScript classes (yes JS is functional language, but you can still make those) that represent your UI layer and handle UI events. Then create your own Ajax library for getting JSON serialized data back and forth from the UI classes to the server back end. Finally create your own business logic classes (also in javascript) to control your UI and DataAccess (ajax) classes. In short.... lots of dirty work.

like image 25
bleepzter Avatar answered Nov 04 '22 10:11

bleepzter


There is also the experimental Model Driven Views (MDV) library from Google. Unfortunately, it is only intended as a prototype showcase. Conceptually, it is really cool, but be prepared to do some bug fixing on your own.

http://code.google.com/p/mdv/

like image 41
Jack Wester Avatar answered Nov 04 '22 10:11

Jack Wester