简体   繁体   English

Knockout.js中的“模型”和“ViewModel”

[英]'Model' and 'ViewModel' in Knockout.js

In MVC, ' Model ' is just code representation of data (eg in ASP.NET MVC it's a class with according fields). 在MVC中,“ 模型 ”只是数据的代码表示(例如,在ASP.NET MVC中,它是具有相应字段的类)。

In Knockout however (which employs MVVM), I see that object with fields is called a 'ViewModel'. 然而在Knockout(使用MVVM)中,我看到带有字段的对象称为“ViewModel”。 From official KO documentation: 从官方KO文件:

A model: your application's stored data. 模型:应用程序的存储数据。 This data represents objects and operations in your business domain (eg, bank accounts that can perform money transfers) and is independent of any UI. 此数据表示业务域中的对象和操作(例如,可以执行汇款的银行帐户),并且独立于任何UI。 When using KO, you will usually make Ajax calls to some server-side code to read and write this stored model data. 使用KO时,通常会对某些服务器端代码进行Ajax调用,以读取和写入此存储的模型数据。

A view model: a pure-code representation of the data and operations on a UI. 视图模型: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. 例如,如果您正在实现列表编辑器,则视图模型将是一个包含项列表的对象,并公开添加和删除项的方法。

From examples, it can be seen that ViewModels are objects with fields, holding the data, what usually'd be done by Model in MVC: 从示例中可以看出,ViewModel是包含字段的对象,包含数据,通常由Model在MVC中完成的操作:

var myViewModel = {
    personName: ko.observable('Bob'),
    personAge: ko.observable(123)
};

So I'm a little lost here. 所以我在这里有点失落。 What exactly 'Model' and 'ViewModel' mean in Knockout.js domain? 什么'模型'和'ViewModel'在Knockout.js域中意味着什么?

In a JavaScript-implemented MVVM pattern, often the "Model" part is supplied by the web api. 在JavaScript实现的MVVM模式中,“模型”部分通常由web api提供。 The data that is provided to the page is the model. 提供给页面的数据是模型。 Now whether that data is contained in an separate model object once it's in JavaScript - that's another story. 现在,一旦它在JavaScript中,该数据是否包含在单独的模型对象中 - 这是另一个故事。

Often a View Model is just a Model that has been augmented with properties and functions to support the particular view that it is applied to. 通常,视图模型只是一个模型,它已经使用属性和函数进行了扩充,以支持应用它的特定视图。 Client-side calculations, drop-down look-up values, client-side validation routines, etc. In this case, the view model might look like this: 客户端计算,下拉查找值,客户端验证例程等。在这种情况下,视图模型可能如下所示:

var vm = {
    save: function(){ save logic... },
    cancel: function(){ cancel logic... },
    states: ko.observable(), //list of states for state dropdown
    customerId: ko.observable(),
    customerFirstName: ko.observable(),
    customerLastName: ko.observable()
};

Other times, the model will be maintained in a separate object . 其他时候,模型将保持在单独的对象中 In that case the view model might look like this: 在这种情况下,视图模型可能如下所示:

var customerModel = getCustomerFromDataSource();
var vm = {
    save: function(){ save logic... },
    cancel: function(){ cancel logic... },
    states: ko.observable(), //list of states for state dropdown
    customer: customerModel
};

The main thing to keep in mind is that the Model is the data and the View Model is the layer that makes your Model available to the view (usually via data-binding). 要记住的主要事情是模型是数据,视图模型是使模型可用于视图的层(通常通过数据绑定)。 Sometimes the Model may be a separate class; 有时模型可能是一个单独的类; other times the Model is just a known set of properties in the View Model. 其他时候,模型只是视图模型中的一组已知属性。

Model 模型
Models hold information, but typically don't handle behaviour 模型包含信息,但通常不处理行为

View 视图
View contains the data-bindings, events and behaviours which require an understanding of the Model and ViewModel. View包含需要了解Model和ViewModel的数据绑定,事件和行为。 Although these behaviours can be mapped to properties, the View is still responsible for handling events to the ViewModel 虽然这些行为可以映射到属性,但View仍然负责处理ViewModel的事件

ViewModel 视图模型
ViewModel sits behind our UI layer. ViewModel位于我们的UI层后面。 It exposes data needed by a View (from a Model) and can be viewed as the source our Views go to for both data and actions. 它公开View(从模型)所需的数据,并且可以被视为我们的视图用于数据和操作的源。

You can find out more information in the following link here 您可以在此处的以下链接中找到更多信息
You can also find more information about mvc and mvvm in stackoverflow question 您还可以在stackoverflow问题中找到有关mvc和mvvm的更多信息

Well this was told by my Professor (also a JavaScript programmer). 好吧,这是我的教授(也是一名JavaScript程序员)讲的。

Model is an object which holds data as one of its property. Model是一个将数据保存为其属性之一的对象。

whereas ViewModel is just an interface/layer separating the data and the DOM (Document object model). 而ViewModel只是一个分隔数据和DOM(Document对象模型)的接口/层。

In case of Model, the data tries to connect to the user Interface each time a document object model is called. 对于Model,每次调用文档对象模型时,数据都会尝试连接到用户界面。

ViewModel is used to stop that practice. ViewModel用于停止该练习。 Once the data in the model is completely ready, it's been assigned to an array present in the viewModel. 一旦模型中的数据完全就绪,它就被分配给viewModel中存在的数组。 Then from the view model it is displayed into the User interface. 然后从视图模型中将其显示到用户界面中。

It's just a good way of programming. 这只是一种很好的编程方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM