MVVM在DHTML RIA应用程序中是否可行/可行(没有Silverlight/WPF)?

Ami*_*mir 17 javascript asp.net-mvc json dhtml mvvm

注意:这是一个冗长的问题,需要很好地理解MVVM"设计模式",JSON和jQuery ....

所以我有一个理论/声称DHTML中的MVVM是可行可行的,并想知道你是否同意/不同意我以及为什么.在DHTML中实现MVVM围绕使用对服务器实体的ajax调用,该服务器实体返回JSON,然后通过javascript使用html操作来控制html.

所以要打破它.可以说我正在构建一个搜索数据库中的人物搜索页面.....

视图会是这个样子:

    
        <body viewmodel="SearchViewModel">
            Search:<br />
            <input type="text" bindto="SearchString" /><br />
            <input type="button" value="Search" command="Search" />
            <br />
            <table bindto="SearchResults">
                <thead>
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>${FirstName}</td>
                        <td>${LastName}</td>
                    </tr>
                </tbody>
            </table>
        </body>
    

在我的html元素上使用了一些非标准属性,我已经明确地定义了一个View以及它将如何与我的ViewModel交互.我在javascript中创建了一个MVVM解析器,它解释了非标准属性,并将View与代表ViewModel的JSON对象相关联.

视图模型将是一个JSON对象:

    
        //View Model SearchViewModel would be assocaited with View because of <body viewmodel="SearchViewModel">
        var SearchViewModel = {
            //SearchString variable has a TwoWay binding 
            //to <input type="text" bindto="SearchString" /><
            //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 MVVM parser
            SearchString: '',

            //SearchResults has a OneWay binding to <table bindto="SearchResults">
            SearchResults: new Array(), 

            //Search function will get "auto-magically" get called because of 
            //the command binding to <input type="button" command="Search" />
            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;
                //simulate INotifyPropertyChanged using the MVVM parser
                mvvmParser.notifyPropertyChanged("SearchResults");
            }
        }
    

模式可以是任何服务器端的资产,返回JSON ......在这个例子中,我用ASP MVC作为一个宁静的门面:

    
        public JsonResult SearchForPerson(string searchString)
        {
            PersonDataContext personDataContext = new PersonDataContext(); //linq to sql.....

            //search for person
            List<Person> results = 
                personDataContext.Persons
                                 .Where(p => p.FirstName.Contains(searchString)
                                             || p.LastName.Contains(searchString))
                                 .ToList();

            return Json(results);
        }
    

那么,问题又一个:
MVVM在DHTML RIA应用程序中是否可行/可行(没有Silverlight/WPF)或者我是否已经忘记了?

这个"MVVM框架"可能是个好主意吗?

概念证明:kaboom.codeplex.com.

Rya*_*ett 10

这可能是链接到淘汰JS的好时机,这是一个javascript mvvm框架.

您可能还想看一下javascript MVC框架的骨干: