1

I was wondering what is considered best practice. Let's say I have a dropdown select widget. Should it be preloaded with content when the page is served up from the server or once it is loaded, should an AJAX request be made to retrieve the contents and then populate it?

I kinda like the idea of loading it empty and issuing an AJAX call to retrieve the contents. But is that going to result in slower page loading times. Especially if the page has several widgets that need to be loaded with content from the server?

Does it matter on the amount of content being loaded?

For the dropdown example, I meant the options in the dropdown. So if I had a dropdown of employees to choose, from I am referring to that list of employees. Do I load an empty dropdown and on init of the controller have it retrieve the employees and populate the dropdown?

But then I think about a datagrid of let's say 200 rows of emplyees and some employee data that is stored in a database. Do I load the page and when the page loads have a controller whose init function retrieves the dataset of employees and populates and displays the datagrid?

Or when the page is served up from the server does it retrieve the dataset on the server-side where it also creates the datagrid and it gets loaded then. This is the programming world that I am used to having done mostly PHP, JSP and ASP stuff in the past. Only using JavaScript for some cool page effects, etc.

But I seem to like the idea of once the page is loaded (or being loaded), make AJAX requests to retrieve the data needed to populate the widgets/content on the current screen. I am just concerned that the loading of page might seem clunky or slow since I am now making more requests to the server to paint the page. The initial request for the page, and then a request for each dataset needed to populate a widget.

4

3 回答 3

3

我认为最好的答案是“这取决于你的框架”。我见过以两种方式处理此问题的 Web 框架。我确实认为,特别是当大量数据被填充到 DOM 中时,从性能的角度来看,在初始 HTTP 响应中加载尽可能多的页面是更可取的,并且只根据需要通过 AJAX 进行更新。请注意,对于大型数据集,与通过 JavaScript 执行大量 DOM 操作的影响相比,额外 HTTP 请求的性能开销相对较小——在某些浏览器中,DOM 操作可能非常慢。

如果您添加您正在使用的框架,您可能会得到更详细的答案。

于 2011-01-28T20:36:07.623 回答
1

正如您自己指出的那样,使用 AJAX 加载所有内容会使页面变慢。事实上,加快网页速度的最佳实践之一就是减少 http 请求的数量。

话虽如此,在某些情况下,AJAX 会使页面加载更快。一个例子是文本框的自动完成。如果在不使用 AJAX 的情况下实现这样的功能,则需要网页在第一次加载时加载每个可能的查询。该页面将永远加载所有这些数据。但是一旦页面加载,自动完成可能会比正常响应更多。

我的建议是从你能想到的最简单的解决方案开始,然后在需要的地方添加 AJAX、缓存和其他优化功能。

于 2011-01-28T20:33:38.190 回答
0

这取决于您显示的数据,如果您想从数据库快速更新,ajax 方法很好。但是,如果您的数据是固定的(或者您不关心在页面加载后更新它),那么我不应该使用 ajax 方法,更多的工作(请求 json,解析它,填充选择或其他)和你不会获得太多好处。

于 2011-01-28T20:34:00.340 回答