剔除viewmodel概念 [英] Knockout viewmodel concept

查看:91
本文介绍了剔除viewmodel概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到以下答案...请给我一个建议.
我有一种形式是基于通过ajax请求从服务器获取的元数据来动态构建的表单.它获得约20个显示数据值以及约10个用户输入字段.因此,演示视图模型和后视图模型是不同的.填充的字段也会通过ajax回发.
如何正确应用Knockout视图模型概念?
1.我制作了一个用于显示和发布数据的视图模型.在这种情况下,AJAX调用会将大量冗余数据回发到服务器.选项:我可以发送一个仅包含输入字段的新对象,但在KO概念中看起来并不好.
2.我制作了一个仅包含用于用户输入的字段的viewmodel.要显示的只读字段不在KO视图模型中,而是使用常见的jQuery方法填充(因此我们又脱离了纯KO样式)
3.还是?
感谢您的想法.

I was not able to find an answer for the following... please gimme an advice.
I have a form that is built dinamically basing on metadata being obtained from server via ajax request. It gets about 20 values for display data and also about 10 fields for user input. Thus, the presentation view model and post view model are different. Filled fields are posted back via ajax as well.
How do i apply Knockout view models concept correctly?
1. I make a single viewmodel for displaying and posting data. In this case AJAX call will post back a lot of redundant data to server. Option: i can send a new object that will contain only input fields, but it doesnt look OK in KO concept.
2. I make a single viewmodel containing only fields for user input. Read-only fields to display stay out of KO view model and populated using common jQuery methods (so we're out of pure KO style again)
3. Or?
I appreciate your ideas.

推荐答案

Knockout提供了将MVVM模式应用于客户端(HTML5/Javascript)应用程序的功能.您的JavaScript视图模型应该为用户输入字段和仅显示字段提供操作视图所负责的所有数据和属性.

Knockout provides the ability to apply MVVM pattern to a client-side (HTML5/Javascript) app. Your JavaScript view model should provide all the data and properties necessary to operate the view or views that it is responsible for, both for user input fields and display-only fields.

一旦您将某些内容发布回服务器,您将离开MVVM世界,并进入另一层以执行某些操作.因此,我认为最好制定仅包含服务器完成请求所需数据的JSON.在服务器端,您可能具有带有验证属性或其他属性的C#模型,但是,再次-您不想在那里遵循MVVM模式.

Once you post something back to the server, you're leaving the MVVM world and reaching into another layer to perform some operation. As a result, I think it's best to formulate JSON that contains only the data that the server needs to complete the request. On the server side, you may have a C# model with validation attributes or whatever, but, again - you're not trying to adhere to MVVM pattern there.

希望这会有所帮助.如果需要,我很乐意详细说明.

Hopefully this helps. I'm happy to elaborate if needed.

这篇关于剔除viewmodel概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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