Web 应用程序架构:使用服务器作为数据和模板存储库在浏览器中实现 MVC
使用以下架构实现 Web 应用程序的优点和缺点是什么:
简单的服务器,仅提供数据的 JSON 响应,以及带有要在客户端中呈现的模板的 HTML。 客户端是一个 JavaScript MVC“应用程序”,它接收并缓存 HTML 模板,并根据需要请求 JSON 格式的数据。 js 是应用程序的核心和逻辑,实现所有的行为和交互。
在此特定场景中,服务器将要求作为数据存储库和 HTML 模板服务器。客户端接收模板并缓存它们以供稍后(使用离线存储,为什么不)在浏览器本身中使用 JSON 数据呈现它们。
我可以看到这样做的一些优点:
- 浏览器仅请求必要的数据和模板,使其能够以非常低的带宽工作。缓存模板是一个很大的优点。
- 服务器非常简单,甚至可以使用 Node.js 在 javascript 中实现,以便通过在服务器中渲染模板并在响应中发送生成的 html 来为禁用 javascript 的浏览器提供优雅的回退。
和一些缺点:
- 由于模板和数据是单独请求的,所以会发出更多请求。
- 在这个想法的基本应用程序中,如果用户禁用了 javascript,它就不起作用。
我已经思考这个问题有一段时间了,我不知道这个想法有多疯狂,也不知道当前是否有这个实现,或者即使它有一个名字。有什么额外的优点或缺点吗?有现有的实现吗?
如果这个想法确实有名字并且存在相关问题,请随意关闭它。
如果我有代表,我会把它变成一个社区维基,但我不能。
What would be the advantages and disadvantages of implementing a web application with the following architecture:
Simple server that simply serves JSON responses of data, and HTML with templates to be rendered in the client.
The client side be a javascript MVC "application" that receives and caches HTML the templates and asks for data in JSON format as needed. The js is the heart and logic of the application, implementing all the behaviour and interaction.
In this particular scenario, the server would ask as a repository of data and as a server of HTML templates. The client receives the templates and caches them for later (using offline storage, why not) rendering them with the JSON data in the browser itself.
I can see some upsides to this:
- The browser only requests just the necessary data and templates, making it work with a very low bandwidth. Caching the templates is a big plus.
- The server is quite simple, and may even be implemented in javascript using Node.js to allow for a graceful fallback for browsers that have javascript disabled by rendering the templates in the server and sending the generated html in the response.
and some downsides:
- More requests are made as templates and data are requested separately
- In a bare bones application of this idea, it doesn't work if the user has javascript disabled.
I've been thinking about this for a while, and I don't know how crazy this idea is, and if there are current implementations of this, or even if this has a name. Any extra advantages or disadvantages? Any existing implementations?
Feel free to close it if this idea does indeed have a name and a question exists about it.
If I had the rep, I'd make this a community wiki, but I can't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linkedin 的工程团队目前正在撰写博客,介绍他们如何从服务器端模板迁移到客户端 JavaScript 模板。
已经有几篇文章
Linkedin's engineering team is currently blogging about how they moved from server-side templates to client-side JavaScript templates.
There's already several articles available
我不知道是否存在这样的事情。
但是,我没有发现对服务器的请求有真正的好处,就像通常的服务器端 MVC 一样,您只能下载一次模板并仅请求 JSON 数据(也许将模型的一些行为转移到 JS)。
另外,请记住,向服务器发出 10 个 1kb 数据请求将比 1 个 10kb 数据请求花费更长的时间。如果您的请求较多且数据较少,则可能会遇到较高的延迟。
我发现这个想法的一个很大的缺点是你暴露了所有的代码。你可能会混淆它,但它仍然存在,任何人都可以看到它。
一个很大的好处是:如果您使用最少的数据实现一些离线存储,它可能可以离线工作。
I don't know if something like this exists.
However, I don't find a real gain in requests to the server, as with the usual server-side MVC you can download the template only once and request just the JSON data (perhaps moving some of the model's behaviour to JS).
Also, remember that 10 requests of 1kb of data to the server will take longer than 1 request of 10kb. If you have more requests with less data you may experience higher delays.
One big downside that I find to this idea is that you are exposing all your code. You may obfuscate it, but it is still there for anyone to see it.
A big upside: it might work offline if you implement some offline storage with a minimum of data.
老问题,但我看到这里没有人提到 Bones——构建在 NodeJS 和 BackboneJS 上,来完成你正在寻找的事情。
来自网站:
https://github.com/developmentseed/bones
Old question, but I saw that nobody here mentioned Bones—built on NodeJS and BackboneJS, to do just what you're looking for.
From the website:
https://github.com/developmentseed/bones