gpt4 book ai didi

javascript - MVVM 在 DHTML RIA 应用程序(无 Silverlight/WPF)中是否可能/可行?

转载 作者:可可西里 更新时间:2023-11-01 01:18:32 29 4
gpt4 key购买 nike

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

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

所以分解一下。假设我正在构建一个搜索页面,用于在数据库中搜索人员......

View 看起来像这样:

    
<body <strong>viewmodel="SearchViewModel"</strong>>
Search:<br />
<input type="text" <strong>bindto="SearchString"</strong> /><br />
<input type="button" value="Search" <strong>command="Search"</strong> />
<br />
<table <strong>bindto="SearchResults"</strong>>
<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 对象相关联。

ViewModel 将是一个 JSON 对象:

    
//View Model SearchViewModel would be assocaited with View because of <body <strong>viewmodel="SearchViewModel"</strong>>
var SearchViewModel = {
//SearchString variable has a TwoWay binding
//to <input type="text" <strong>bindto="SearchString"</strong> /><
//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 <table <strong>bindto="SearchResults"</strong>>
SearchResults: new Array(),

//Search function will get "auto-magically" get called because of
//the command binding to <input type="button" <strong>command="Search"</strong> />
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");
}
}

Model 可以是任何返回 JSON 的服务器端 Assets ...在这个例子中,我使用 asp MVC 作为 restful facade:

    
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 .

最佳答案

这可能是链接到 knockout JS 的好时机, 这是一个 javascript mvvm 框架。

您可能还想看看 backbone ,一个 JavaScript MVC 框架:

关于javascript - MVVM 在 DHTML RIA 应用程序(无 Silverlight/WPF)中是否可能/可行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132927/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com