简体   繁体   English

处理Javascript Server-Side是否是重复逻辑的解决方案?

[英]Is processing Javascript Server-Side a solution to duplicated logic?

Web-Applications these days make extensive use of Javascript, for example various Google Products like Gmail and Calendar. 如今,Web应用程序广泛使用Javascript,例如Gmail和日历等各种Google产品。

I'm struggling to how NOT having duplicated logic server and client side. 我正在努力解决如何没有重复的逻辑服务器和客户端。

When requesting a page or state of the application, i would prefer to send the complete UI, meaning: not just some javascript, which in turn makes a dozen ajax requests and builds the user interface. 在请求应用程序的页面或状态时,我更愿意发送完整的UI,这意味着:不仅仅是一些javascript,它反过来会产生十几个ajax请求并构建用户界面。

But here lies the problem, the logic deciding what to show or not has to be written once in the server-side and once in the client-side language. 但问题在于,决定要显示与否的逻辑必须在服务器端编写一次,在客户端语言编写一次。

Then i was wondering if it was somehow possible to process your javascript logic server-side and send the whole to the client, who in turn can continue using the application with all the advantages of a responsive ui, but without disadvantage of the initial loading/building of the user interface due dependency of background ajax requests. 然后我想知道是否有可能以某种方式处理你的javascript逻辑服务器端并将整个发送到客户端,而客户端又可以继续使用具有响应ui的所有优点的应用程序,但没有初始加载的缺点/由于后台ajax请求的依赖性而构建用户界面。

I hope the explanation of my problem is a bit clear, because i'm not the most fluent English writer. 我希望我的问题的解释有点清楚,因为我不是最流利的英语作家。 If you understand what i mean and if you can describe the problem a little better, please do... thanks! 如果你理解我的意思,如果你能更好地描述问题,请做...谢谢!

So my question is: 所以我的问题是:

  • Is something like this possible and or realistic? 这样的事情是可能的还是现实的?
  • What is your opinion on how to tackle this problem? 您对如何解决这个问题有何看法?

;-) ;-)

When we started our web app, we had the same kind of questions. 当我们启动我们的网络应用程序时,我们遇到了同样的问题。
It may help you to know how we ended: 它可能会帮助您了解我们的结果:

  • The backend (business logic, security) is totally separated from the frontend (gui) 后端(业务逻辑,安全性)与前端完全分离(gui)

  • frontend and backend communicate through JSON services exclusively 前端和后端专门通过JSON服务进行通信

  • the JSON is rendered client-side with the PURE templating library 使用PURE模板库在JSON中呈现客户端

  • and the backend is Erlang (anything streaming JSON would be ok too, but we liked its power) 而后端是Erlang(任何流JSON都可以,但我们喜欢它的力量)

And for your question, you have to consider the browser as totally unsafe. 对于您的问题,您必须将浏览器视为完全不安全。
All the security logic must come from the backend. 所有安全逻辑都必须来自后端。

Hiding or showing some parts of the screen client side is ok, but for sure the backend decides which data is sent to the browser. 隐藏或显示屏幕客户端的某些部分是可以的,但是确保后端决定将哪些数据发送到浏览器。

Seems you describe Jaxer .You can write everything in JS. 好像你描述了Jaxer 。你可以用JS编写所有内容。
Also, there is GWT that allows to write whole thing on Java 此外,还有GWT允许在Java上编写完整的东西

Then i was wondering if it was somehow possible to process your javascript logic server-side and send the whole to the client, who in turn can continue using the application with all the advantages of a responsive ui, but without disadvantage of the initial loading/building of the user interface due dependency of background ajax requests. 然后我想知道是否有可能以某种方式处理你的javascript逻辑服务器端并将整个发送到客户端,而客户端又可以继续使用具有响应ui的所有优点的应用程序,但没有初始加载的缺点/由于后台ajax请求的依赖性而构建用户界面。

Maybe the apps you're looking at just use Ajax poorly. 也许您正在查看的应用程序只是使用Ajax。

The only content you can pre-process on the server is the content you already know the user wants. 您可以在服务器上预处理的唯一内容是您已经知道用户想要的内容。 For example, in an email app, you could send them a complete view of their inbox, pre-processed on the server and fetched with a single request, as soon as they log in. But you might use AJAX to fetch a particular message once they click on it. 例如,在电子邮件应用程序中,您可以在他们登录后立即向他们发送收件箱的完整视图,在服务器上预处理并使用单个请求获取。但您可以使用AJAX获取特定邮件一次他们点击它。 Sending them all the messages up front would be too slow. 预先向他们发送所有消息将会太慢。

Used correctly, AJAX should make your pages faster , because it can request tiny updates or changes of content without reloading the whole page. 如果使用得当,AJAX应该可以让您的页面更快 ,因为它可以请求微小的更新或更改内容而无需重新加载整个页面。

But here lies the problem, the logic deciding what to show or not has to be written once in the server-side and once in the client-side language. 但问题在于,决定要显示与否的逻辑必须在服务器端编写一次,在客户端语言编写一次。

Not necessarily. 不必要。 For example, in PHP, you might write a function like displayWidgetInfo() . 例如,在PHP中,您可以编写一个类似displayWidgetInfo()的函数。 You could use that function to send the initial widget information at page load. 您可以使用该函数在页面加载时发送初始窗口小部件信息。 If the user clicks the widget to change something, send an AJAX request to a PHP script that also uses displayWidgetInfo() to send back new results. 如果用户单击窗口小部件以更改某些内容,请将AJAX请求发送到也使用displayWidgetInfo()发送回新结果的PHP脚本。 Almost all your logic stays in that single function. 几乎所有逻辑都保留在单一功能中。

Your instincts are correct : it's bad to duplicate code, and it's bad to make too many requests for one page. 你的直觉是正确的 :重复代码是不好的,而且对一个页面提出太多请求是不好的。 But I think you can fix those problems with some refactoring. 但我认为你可以通过一些重构解决这些问题。

I understand what you're saying. 我明白你在说什么。

But I don't think you should be having much 'logic' about what to build, on the client side. 但我不认为你应该在客户端有很多关于构建内容的“逻辑”。 If you did want to go with a model like you're proposing (not my cup of tea, but why not), I don't see why you'd end up with much duplicated. 如果你确实想要使用像你提出的模型(不是我的一杯茶,但为什么不这样),我不明白为什么你最终会有很多重复。

Where you would normally show a table or div , you would just output JavaScript, that would build the relevant components on the client side. 在通常显示tablediv ,您只需输出JavaScript,即可在客户端构建相关组件。

I would consider it just as another 'View' into your data/business logic model. 我会将其视为您的数据/业务逻辑模型中的另一个“视图”。

Have you go a small example of a problem you're coming up against? 你有一个小问题,你遇到了一个问题吗?

I understand your question in this way: Suppose we have an html form on web-page. 我用这种方式理解你的问题:假设我们在网页上有一个html表单。 There is a field for name and surname. 名字和姓氏都有一个字段。 We have to check it for validity both on client-side (with JS) and Sever-side (on php script while processing form inputs). 我们必须在客户端(使用JS)和Sever端(在处理表单输入时使用php脚本)检查它的有效性。 So here is the duplication - regex check on both sides. 所以这里是复制 - 双方的正则表达式检查。 So what is the way to prevent it and combing these logics? 那么防止它并梳理这些逻辑的方法是什么?

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

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