Note that there are some explanatory texts on larger screens.

plurals
  1. POIs MVVM possible/viable in a DHTML RIA application (no Silverlight/WPF)?
    primarykey
    data
    text
    <p>Note: This is a long winded question and requires a good understanding of the MVVM "design pattern", JSON and jQuery....</p> <p>So I have a theory/claim that MVVM in DHTML is <strong>possible</strong> and <strong>viable</strong> and want to know if you agree/disagree with me and why. Implementing MVVM in DHTML revolves around using ajax calls to a server entity that returns JSON and then using html manipulation via javascript to control the html.</p> <p>So to break it down. Lets say I'm building a search page that searches for People in a database.....</p> <p>The <strong>View</strong> would look something like this:</p> <pre> <code> &lt;body <strong>viewmodel="SearchViewModel"</strong>&gt; Search:&lt;br /&gt; &lt;input type="text" <strong>bindto="SearchString"</strong> /&gt;&lt;br /&gt; &lt;input type="button" value="Search" <strong>command="Search"</strong> /&gt; &lt;br /&gt; &lt;table <strong>bindto="SearchResults"</strong>&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;${FirstName}&lt;/td&gt; &lt;td&gt;${LastName}&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/body&gt; </code> </pre> <p>Using some non standard attributes on my html elements, I have declaritively defined a <strong>View</strong> and how it will interact with my <strong>ViewModel</strong>. I've created a <strong>MVVM parser in javascript</strong> that interprets the non-standard attributes and associates the View with a JSON object that represents the ViewModel.</p> <p>The <strong>ViewModel</strong> would be a JSON object:</p> <pre> <code> //View Model SearchViewModel would be assocaited with View because of &lt;body <strong>viewmodel="SearchViewModel"</strong>&gt; var SearchViewModel = { //SearchString variable has a TwoWay binding //to &lt;input type="text" <strong>bindto="SearchString"</strong> /&gt;&lt; //if a user types into the text box, the SearchString property will "auto-magically" be updated //because of the two way binding and how the element was interpreted via my <strong>MVVM parser</strong> SearchString: '', //SearchResults has a OneWay binding to &lt;table <strong>bindto="SearchResults"</strong>&gt; SearchResults: new Array(), //Search function will get "auto-magically" get called because of //the command binding to &lt;input type="button" <strong>command="Search"</strong> /&gt; Search: function() { //using jquery to call into the server asynchronously //when the server call is completed, the PopulateSearchResults method will be called $.getJSON("www.example.com/SearchForPerson", { searchString: SearchViewModel.SearchString }, SearchViewModel.PopulateSearchResults); } PopulateSearchResults: function(data) { //set the JSON array SearchViewModel.SearchResults = data; //<strong>simulate INotifyPropertyChanged using the MVVM parser</strong> mvvmParser.notifyPropertyChanged("SearchResults"); } } </code> </pre> <p>The <strong>Model</strong> can be any server side asset that returns JSON...in this example, I used asp MVC as a restful facade:</p> <pre> <code> public JsonResult SearchForPerson(string searchString) { PersonDataContext personDataContext = new PersonDataContext(); //linq to sql..... //search for person List&lt;Person&gt; results = personDataContext.Persons .Where(p => p.FirstName.Contains(searchString) || p.LastName.Contains(searchString)) .ToList(); return Json(results); } </code> </pre> <p>So, again the question:<br> <strong>Is MVVM possible/viable in a DHTML RIA application (no Silverlight/WPF) or have I lost my mind?</strong></p> <p><strong>Could this "MVVM framework" be a good idea?</strong></p> <p><strong>Proof of Concept: <a href="http://kaboom.codeplex.com" rel="noreferrer">kaboom.codeplex.com</a>.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload