使用MVVM与Knockout.js当服务器端定义模型 [英] Defining models on server side when using MVVM with Knockout.js

查看:111
本文介绍了使用MVVM与Knockout.js当服务器端定义模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用knockout.js和MVVM模式在客户端上的一个单页的应用程序。所以模型,的ViewModels将在客户端来限定。我感到困惑,我们怎么也得在服务器端结构。


  1. 现在,将控制器只返回域模型本身?都会从域模型视图模型来映射在客户端只发生?


  2. 在我的解决方案,还有就是域模型和视图模型之间的巨大差距。这样将导致上述方法在大量的数据被返回给客户端不必要地。虽然它似乎有点小题大做,我想在服务器端重复视图模型和InputViewModel定义(前重presents数据呈现,后者再presents数据被调回控制器动作),也有一个映射层(基于automapper),以领域模型映射到服务器端的ViewModels。这是否有意义?还是有更好的方法?



解决方案

我建议你锻炼你的视图模型实际需要的数据,然后让控制器建立一个包含服务器端的视图模型的数据和发送它以一个JSON格式的客户端。

你是不是在向客户端发送不必要的数据(或背面)这样,你仍然可以做很多服务器和淘汰赛视图模型上繁重的工作可以做他们的意思为:presenting将数据发送到视图中使用。

I am planning to use knockout.js and MVVM pattern on the client side for a Single Page application. So Models, ViewModels would be defined on the client side. I am confused about how we have to structure on the server side.

  1. Now, would controllers just return domain model itself ? Would all the mapping from domain model to ViewModel happen only on the client side ?

  2. In my solution, there is wide gap between the Domain model and the ViewModel. So the above approach would result in a lot of data being returned to client side unnecessarily. Though it seems like overkill, I am thinking about repeating ViewModel and InputViewModel definitions (former represents data rendered, latter represents data to be posted back to controller actions) on server side and also having a mapping layer (based on automapper) to map Domain models to ViewModels on the server side. Does this make sense ? Or is there a better approach ?

解决方案

I would suggest that you work out what data your view models actually need, then have the controllers build up a server-side view model that contains that data and send it to the client in a JSON format.

This way you are not sending unnecessary data over to the client (or back), you are still able to do much of the heavy lifting on the server and the knockout view models can do what they are meant for: presenting the data to be used by the view.

这篇关于使用MVVM与Knockout.js当服务器端定义模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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