简体   繁体   English

在服务器上渲染部分视图或在客户端上发送json数据和渲染模板

[英]Render partial view on the server or send json data and render template on the client

I was wondering what would be a good approach (or recommended approach) to render partial views in a web application. 我想知道在Web应用程序中渲染部分视图的好方法(或推荐方法)。

I have a requirement where I need to load data into an already rendered page using AJAX, sort of like a "Load more..." link at the end of the page which grabs more information from the server and renders it to the bottom of the page. 我有一个要求,我需要使用AJAX将数据加载到已经呈现的页面中,有点像页面末尾的“加载更多...”链接,它从服务器获取更多信息并将其呈现到底部这页纸。

The two options I am playing with at the moment for the AJAX response are 我正在玩AJAX响应的两个选项是

  1. Return a JSON representation of the data, and use a client side template library, (eg jQuery templates) or just plain javascript to turn the JSON into HTML and append to the bottom of the page 返回数据的JSON表示,并使用客户端模板库(例如jQuery模板)或只是简单的javascript将JSON转换为HTML并附加到页面底部
  2. Render the partial view on the server (in my case using grails' render template:'tmplt_name' ) and send it across the wire and just append the result to the bottom of the page 渲染服务器上的局部视图(在我的例子中使用grails' render template:'tmplt_name' )并通过网络发送它,只需将结果附加到页面底部

Are there other ways to do this? 还有其他方法吗? If not, given the above options which would be better in terms of maintenance, performance and testability? 如果没有,鉴于上述选项在维护,性能和可测试性方面会更好? One thing I am certain about is that the JSON route will (in most cases) use up less bandwidth than sending html across the wire. 我确定的一件事是,JSON路由(在大多数情况下)比使用线路发送html消耗更少的带宽。

This is actually a really interesting question, because it exposes some interesting design decisions. 这实际上是一个非常有趣的问题,因为它暴露了一些有趣的设计决策。

I prefer rendering partial templates because it gives my applications the ability to change with time. 我更喜欢渲染部分模板,因为它使我的应用程序能够随时间变化。 If I need to change from a <table> to a <div> with a chart, it's easy to encapsulate that in a template. 如果我需要使用图表从<table>更改为<div> ,则可以很容易地将其封装在模板中。 With that in mind, I view almost every page as a aggregate of many small templates, which could change. 考虑到这一点,我将几乎每个页面视为许多小模板的集合,这些模板可能会发生变化。 Grails 2.0 default scaffolding has moved towards this type of approach, and it's a good idea. Grails 2.0默认的脚手架已经转向这种方法,这是一个好主意。

The question as to whether they should be client-side templates or server-side is the crux of the issue. 关于它们应该是客户端模板还是服务器端的问题是问题的症结所在。

Server side templates keep your markup cleaner on initial page load. 服务器端模板可在初始页面加载时保持标记清晰。 Even if you use something like Mustache of ICanHazJS , you kinda sorta need to have an empty element in the page (something with your template), style it appropriately, and work with it in Javascript in order to get update your info. 即使你使用类似于ICANHazJS的 Mustache之类的东西 ,你也需要在页面中有一个空元素(与你的模板有关),适当地设置它,并在Javascript中使用它以便更新你的信息。

Drawbacks 缺点

  • "chatty" applications “聊天”应用程序
  • larger envelopes across the wire (responses include HTML, which may be considered static) 电线上的较大信封(响应包括HTML,可能被视为静态)
  • slower UI response time UI响应时间较慢

Benefits 优点

  • Good for people with lots of server side language experience 适合拥有大量服务器端语言经验的人士
  • Maybe easier to manipulate or modify in a server-side environment (eg return a page embedded with multiple templates, which are programmatically loaded and included) 可能更容易在服务器端环境中操作或修改(例如,返回嵌入了多个模板的页面,这些模板以编程方式加载并包含在内)
  • Keeps most of the application stuff "in one place" 将大部分应用程序内容保存在“一个地方”

Client-side templates can really reduce server-load, however. 但是,客户端模板确实可以减少服务器负载。 They make an application "less -chatty", in that potentially you could minimize the number of calls back to the server by sending larger collections of JSON back (in the same number of bytes, or fewer, that would be taken up by HTML in a server-side template scheme). 它们使应用程序“不那么”,因为可能通过发送更大的JSON集合(在相同的字节数或更少的数量,将由HTML占用)来最小化返回服务器的调用次数。服务器端模板方案)。 They also make UI experience really fast for users, as clicking an "update" link doesn't have to do an AJAX round trip. 它们还为用户提供了非常快的UI体验,因为点击“更新”链接不需要进行AJAX往返。 Some have said: 有人说:

Anthony Eden @aeden 10 Dec Reply Retweeted Favorite · Open the future of web apps: requests are handled by functions, logic is always asynchronous, and HTML is never generated on the server. Anthony Eden @aeden 10月12日回复转推收藏·打开网络应用的未来:请求由功能处理,逻辑始终是异步的,并且从不在服务器上生成HTML。

Drawbacks 缺点

  • Not great for SEO (un-semantic extraneous UI elements on initial page load) 对于SEO(初始页面加载时无语义无关的UI元素)不太好
  • Requires some Javascript foo (to manipulate elements) 需要一些Javascript foo(操纵元素)

Benefits - Responsive - Smaller envelopes 优点 - 响应 - 更小的信封

Trends seem to be moving towards client side templates, especially with the power exposed by HTML5 additions (like <canvas> )...but if leveraging them requires you to rely on technologies you're not extremely familiar with, and you feel more comfortable with Grails partials, it may be worthwhile to start with those and investigate refactoring towards client side templates based on performance and other concerns later. 趋势似乎正朝着客户端模板发展,特别是HTML5添加功能(如<canvas> )暴露出来......但如果利用它们,则需要依靠您不熟悉的技术,让您感觉更舒服使用Grails partials,可能值得从这些开始,并根据性能和其他问题调查​​客户端模板的重构。

在我的视图中,第二个选项呈现部分更好,因为如果你得到一个json数据,你应该像设置样式一样操作它,创建元素,设置值和类似的东西,只需在javascript中获得ajax响应后,但如果你渲染局部,你可以在之前设计你的视图并保持准备就绪,只需使用ajax调用导入,这样就没有责任处理响应数据了。

I would say it depends on how much data you're sending across the wire as you put it. 我会说这取决于你在发送时通过网络发送了多少数据。 If you're implementing a "load more" feature then it seems like you wouldn't want to take 5 seconds to load something. 如果您正在实现“加载更多”功能,那么您似乎不想花5秒钟来加载某些内容。 Google images is a good example of how quick that feature should be. Google图片就是该功能应该多快的一个很好的例子。 If you know you'll never have that much data then rendering the partial on the server might be cleaner, but if you're requirements change then it's a hassle to go back to the first approach. 如果你知道你将永远不会拥有那么多数据,那么在服务器上呈现部分可能会更清晰,但如果你的需求发生变化,那么回到第一种方法是一件麻烦事。 So in short, I'd say the first approach allows for more disaster control as far as how long a load more takes to load a large amount of data. 简而言之,我认为第一种方法允许更多的灾难控制,只要加载大量数据需要多长时间。 I'd say it's also general good practice to take load off server when you can, even if it's a little inconvenient on the client side for the developer. 我会说,尽管在开发人员的客户端有点不方便,但是尽可能在服务器上卸载服务器也是一般的好习惯。

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

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