Model View ViewModel/Knockout解决什么问题? [英] What does Model View ViewModel / Knockout solve?

查看:89
本文介绍了Model View ViewModel/Knockout解决什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读遇到Knockout和MVVM的某人的代码.我在这两个主题上都做了一些阅读,但是我对它们真正解决的问题仍然感到困惑,这很可能是因为我还没有构建足够大的应用程序来解决该框架/体系结构所解决的问题.

我花了一些时间来理解此示例代码- http://knockoutjs.com/img /homepage-example.png -从淘汰赛"首页开始.我希望有人可以向我解释,如果不使用Knockout的话,相同的代码会是什么样子,这怎么可能会引起问题.

(SO可能不是此问题的正确平台,所以请告诉我是否还有其他更合适的Exchange).

谢谢!

解决方案

从10,000英尺起

Knockout提供了用HTML编写的 view 和用JavaScript编写的 viewmodel 上相应属性和函数之间的双向数据绑定.

假设您有一个名为 contacts.html 的HTML视图和一个名为 contacts.js 的JavaScript视图模型.这两个在一起将组成一个模块,而敲除将是将它们绑定在一起的粘合剂.

MVVM代表模型视图ViewModel.我在上面解决了后两个问题. 模型只是您世界某个特定角落的JavaScript表示形式,在这种情况下,就是 Contact .

因此,综合以上所述,我们可能具有(就目录结构而言):

  • models \ contact.js
  • views \ contact.html
  • viewmodels \ contact.js

您可以在视图模型中实例化模型,然后使用Knockout将视图绑定到视图模型.

MVVM只是提供了一种很好的方式来分离关注点并最大限度地提高重用性.作为重用的一个示例,您可以根据上下文将联系人视图绑定到许多不同的联系人视图模型.上下文可以是客户端设备的大小,用户的授权配置文件,应用程序的社区"版本与高级"版本等.

通过改进,可以相对隔离地完成MVVM的任何组件,而不会对整个应用程序产生不利影响.因此,分离关注点的价值.

这有意义吗?

I was reading someone's code where I came across Knockout and MVVM. I did some reading on both topics, but I'm still confused as to what problems they really solve, most likely because I just haven't built applications large enough to come across the problems that this framework/architecture solves.

I spent some time to understand this sample code -- http://knockoutjs.com/img/homepage-example.png -- from the Knockout home page. I was hoping if someone could explain to me what the same code would look like if Knockout were not used, and how that could be problematic.

(SO may not be the right platform for this question, so please let me know if there's some other Exchange that's more appropriate).

Thanks!

解决方案

From 10,000 Feet

Knockout provides two-way data-binding between a view written in HTML and corresponding properties and functions on a viewmodel written in JavaScript.

Imagine you have an HTML view called contacts.html and a JavaScript viewmodel called contacts.js. Those two together would make a module, and Knockout would be the glue that binds them together.

MVVM stands for Model View ViewModel. I addressed the latter two above. The model is simply a JavaScript representation of a particular corner of your world, say, in this case, a Contact.

So, bringing together the above, we might have (in terms of directory structure):

  • models\contact.js
  • views\contact.html
  • viewmodels\contact.js

You might instantiate your model inside your viewmodel, and then bind your view to the viewmodel using Knockout.

MVVM simply provides for a great way to separate concerns and maximize reuse. As an example of reuse, you could bind your contacts view to many different contact viewmodels, depending on context. The context could be the size of the client device, a user's authorization profile, a "community" versus a "premium" version of your application, and so on.

Modifying by improvement any of the components of MVVM can be done in relative isolation without adversely affecting the application as whole. Hence the value of separating concerns.

Does that make sense?

这篇关于Model View ViewModel/Knockout解决什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆