将 MVVM 编码模式与 HTML5 结合使用的合理方法是什么
我有一个用 Silverlight 编写的很好的应用程序。为了提供更好的跨浏览器支持,我们正在考虑使用 HTML5 前端进行重写。
从使用 XAML 和 C# 以及 MVVM 编码模式的 Silverlight 迁移到 HTML5 前端的合理方法是什么?
另一方面,如果这不是一个好主意并且应该放弃 MVVM 模式,请解释原因。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Knockout 是一个很棒的 JavaScript 库,可以帮助您编写 MVVM 客户端。您可以在 javascript 中创建视图模型,并使用 html 模板(在 jquery 模板的帮助下)绑定到它们。您的视图模型可以使用 ajax 来检索数据。
它本质上与 Silverlight 开发相同 - 您使用 HTML 代替 XAML,使用 javascript 代替 C#/VB.Net。您甚至可以真正使用相同的 Web 服务...如果您计划稍后提供这两种接口或转换,这可能会有所帮助。
编辑
我想我想补充一点,恕我直言,MVVM 不应该被放弃,因为它确实在分离关注点方面做得很好(我猜如果正确实现的话),但一个巨大的胜利肯定是它让开发人员思考或在心态上或分离责任在一个应用程序中。我在 Web 项目中使用过 Knockout.js,在 Silverlight 中使用过 Caliburn.Micro,并且非常喜欢这种开发风格。
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.
Knockout 是用 HTML/Javascript 实现的 MVVM 模式,具有数据绑定等功能。
http://knockoutjs.com/
如果你想要的东西可以通过 DOM 操作来实现,并且你习惯了和MVVM模式一样,Knockout应该是一个不错的选择。
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.
是的,HTML 5 的 MVVM 模式称为 MVC。最容易使用 MVC3 实现。提供有关 JavaScript 代码的抽象层、对 ViewModel(又名视图)的支持以及视图、数据访问和业务逻辑之间的关注点分离。
最重要的是,验证和 UI 事件处理以简单的方式为您连接,使您能够从各种 HTML 控件的 JavaScript 事件处理程序的复杂性中抽象出来。
但是,如果您严格想要坚持使用 HTML & JavaScript,我建议滚动你自己的 JavaScript 类(是的,JS 是函数式语言,但你仍然可以制作它们)来代表你的 UI 层并处理 UI 事件。然后创建您自己的 Ajax 库,用于从 UI 类到服务器后端来回获取 JSON 序列化数据。最后创建您自己的业务逻辑类(也在 javascript 中)来控制您的 UI 和 DataAccess (ajax) 类。简而言之......很多肮脏的工作。
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.
还有来自 Google 的实验性模型驱动视图 (MDV) 库。
不幸的是,它只是作为原型展示。从概念上讲,它确实很酷,但要准备好自己修复一些错误。
http://code.google.com/p/mdv/
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/
还有 JSView。这个不像 Knockout 那样灵活,但更新 DOM 的速度更快。该项目与 jquery 松散相关,因为其作者是现已废弃的查询模板的幕后黑手。
There is also JSViews. This one is not as slick as Knockout, but it is faster at updating the DOM. The project is loosely related to jquery as its author was behind the now scrapped query templates.