Knockout.js是否真的采用MVVM模式? [英] Does knockout.js really employ MVVM pattern?

查看:72
本文介绍了Knockout.js是否真的采用MVVM模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是敲门游戏js的新手.片刻之后,我读了ko的标题功能. 我不明白是真的是MVVVM吗?因为他们所谈论的只是数据绑定及其简便性.但是我确定MVVM不仅限于数据绑定,不是吗?

I am new to knockout.js. Few moments back I read the headline features of ko. I could not understand is ko really MVVVM? Because all they talk about is data binding and the ease of it. But I am sure MVVM is more than data binding isn't it?

推荐答案

是的,knockout.js确实应用了MVVM模式. 在文档中对其进行了解释:

Yes, knockout.js does apply the MVVM pattern. It's explained in the documentation:

  • 模型:您的应用程序中存储的数据.此数据代表您业务域中的对象和操作(例如可以执行汇款的银行帐户),并且独立于任何用户界面.使用KO时,通常将对一些服务器端代码进行Ajax调用,以读取和写入此存储的模型数据.

  • A model: your application’s stored data. This data represents objects and operations in your business domain (e.g., bank accounts that can perform money transfers) and is independent of any UI. When using KO, you will usually make Ajax calls to some server-side code to read and write this stored model data.

视图模型:UI上数据和操作的纯代码表示.例如,如果您要实现列表编辑器,则视图模型将是一个包含项列表的对象,并公开用于添加和删除项的方法.

A view model: a pure-code representation of the data and operations on a UI. For example, if you’re implementing a list editor, your view model would be an object holding a list of items, and exposing methods to add and remove items.

请注意,这不是用户界面本身:它没有按钮或显示样式的任何概念.它也不是持久数据模型,而是保存用户正在使用的未保存数据.使用KO时,您的视图模型是不包含HTML知识的纯JavaScript对象.通过这种方式使视图模型保持抽象,使其保持简单,因此您可以管理更复杂的行为而不会迷路.

Note that this is not the UI itself: it doesn’t have any concept of buttons or display styles. It’s not the persisted data model either - it holds the unsaved data the user is working with. When using KO, your view models are pure JavaScript objects that hold no knowledge of HTML. Keeping the view model abstract in this way lets it stay simple, so you can manage more sophisticated behaviors without getting lost.

视图:一个可见的交互式UI,表示视图模型的状态.它显示来自视图模型的信息,向视图模型发送命令(例如,当用户单击按钮时),并在视图模型的状态更改时进行更新.

A view: a visible, interactive UI representing the state of the view model. It displays information from the view model, sends commands to the view model (e.g., when the user clicks buttons), and updates whenever the state of the view model changes.

使用KO时,您的视图只是带有声明性绑定的HTML文档,以将其链接到视图模型.另外,您可以使用使用视图模型中的数据生成HTML的模板.

When using KO, your view is simply your HTML document with declarative bindings to link it to the view model. Alternatively, you can use templates that generate HTML using data from your view model.

这篇关于Knockout.js是否真的采用MVVM模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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