从服务器端逻辑使用MVC一个可重用的方式分隔客户端逻辑 [英] Separating client side logic from server side logic in a reusable way using MVC

查看:114
本文介绍了从服务器端逻辑使用MVC一个可重用的方式分隔客户端逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你回答之前,这个问题是复杂的:

Before you answer, this question is complicated:


  1. 我们在asp.net / asp.net的MVC / jQuery的开发,但我愿意用任何框架的任何平台上的解决方案

  2. 我觉得像排序/隐藏列/重新安排列/验证(其中,这是有道理的)逻辑应该是在客户端

  3. 我觉得像搜索/更新DB /运行的工作流应该是(只是因为安全/调试原因)服务器端逻辑

我们正在试图做的是不会写一大堆的JavaScript来处理在不同环境下相同的特征CREATE在我们的UI一团糟。我知道我可以使用JavaScript文件+面向对象的JavaScript,我在寻找,使图案这​​一切更加容易。

What we are trying to do is NOT CREATE A MESS in our UI by writing a bunch of JavaScript to deal with the same feature in different contexts. I understand that I can use a JavaScript file + object oriented JavaScript, I'm looking for the pattern that makes it all easier.

提出的一种解决方案是对客户端和服务器端,在这里我们可以封装在客户端控制器JavaScript功能,然后在网站的不同部分使用它们的MVC模式。但是,这意味着我们有2 MVC实现!

One solution proposed was to have an MVC model on both the client and server side, where we can encapsulate JavaScript functionality in client side controllers, then use them in different parts of the site. However, this means that we have 2 MVC implementations!

这是矫枉过正?你怎么会在这个解决方案扩展?还有什么其他的解决方案有哪些?

Is this overkill? How would you expand on this solution? What other solutions are there?

推荐答案

两架;你应该始终有服务器端验证和客户端验证

On two; you should always have server side validation as well as client side validation

在三样如果你能找到一种方法来操纵数据库的客户端,这将是IM pressive;)

On three; if you can find a way to manipulate the DB on the client side that would be impressive ;)

我不知道ASP.net是如何工作的,所以我只从我的PHP经验之谈。

I don't know how ASP.net works though, so I am solely speaking from my PHP experience.

我会写由服务器和客户端code成对控制。每个控制需要一个形式,客户端逻辑和服务器端逻辑。形式是由您模板引擎写出,客户端逻辑被连接到形式并写入JS和服务器侧逻辑控制器/动作对的地方,操纵模型。很显然,你不希望你的情侣客户端逻辑,以一个具体的行动/控制器,所以一定要定义可以用来跟你的控制接口,而不是...

I would write controls that are paired by server and client code. Each control needs a form, client side logic and server side logic. The form is written out by your templating engine, the client side logic is attached to the form and written in JS and the server side logic is a controller/action pair somewhere that manipulates the model. Clearly, you would not want to couple your client side logic to a specific action/controller, so be sure to define an interface that can be used to talk to your control instead...

然后为每个表单我会的JavaScript情况下你的控件编写的类。例如;你可能有一个控件:

Then for each form I would write a class in javascript that instances your controls. For example; you may have a control:

{include file = "list_view.php" id = "ListView1" data = $Data.List}

这将打印表格出来。然后在你的页面控制器类:

which would print your form out. Then in your page controller class:

this.ListView1 = new ListViewController({id : "ListView1", serverCtrl : "Users"});

现在你可以使用this.ListView1操纵列表视图。列表视图控制器做这样的东西,使新页面AJAX的查询,如果使用presses下页按钮 - 也处理列和排序(这也将委托给服务器)

Now you can use "this.ListView1" to manipulate the list view. The list view controller does stuff like makes AJAX queries for new pages if the use presses the next page button - and also handles columns and sorting (which will also delegate to the server).

这篇关于从服务器端逻辑使用MVC一个可重用的方式分隔客户端逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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