Rails 3 - 在页面加载时进行多个 ajax 调用与进行单个调用

发布于 2024-12-26 02:07:33 字数 393 浏览 1 评论 0原文

我有一个包含三个组件的页面:建议的项目、建议的人员和帖子提要。当此页面上出现 GET 请求时,我有两个选择:

1)在页面的控制器/操作中,我可以获取建议的项目、建议的人员以及构成提要的帖子,然后显示包含所有内容的页面立即获取数据。


2)在页面的控制器/操作中,我没有做太多事情。我可以在页面上使用 ajax 向建议项目的控制器/操作、建议人员的控制器/操作以及帖子提要的控制器/操作发出三个独立的请求,并让他们的 js.erb 文件使用以下内容填充页面:获取的内容。

首先,选项(2)可能吗?如果是这样,它似乎更加模块化,因为我可以为其他页面重用该逻辑,这些页面也需要显示建议的项目、建议的人员等......这显然是以发出更多请求为代价的。

推荐哪种方法?优点/缺点?

谢谢。

I have a page which has three components: suggested items, suggested people, and a post feed. When a GET request is issues on this page, I have two options:

1) In the controller/action for the page, I can fetch the suggested items, suggested people, and the posts making up the feed and then display the page with all the data at once.


2) In the controller/action for the page, I don't do much. I can use ajax on the page to make three independent requests to a controller/action for suggested items, a controller/action for suggested people, and a controller/action for the post feed and let their js.erb files populate the page with the fetched content.

First of all, is option (2) even possible? If so, it seems as if it is more modular since I can reuse that logic for other pages which also need to display suggested items, suggested people, etc... This is obviously at the cost of making more requests.

Which method is recommended? Pros/cons?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2025-01-02 02:07:33

好吧,我建议每天打一个电话,而不是打 3 个单独的电话。正如您所提到的,发出三个不同请求的成本也是如此,另一个原因是某些浏览器上对域的并发连接数的限制(IE7 为 2)。还要考虑您的网络服务器以及随着用户数量的增加它可以处理的最大请求。

此外,您仍然可以通过拥有所有三个操作来使其模块化,并且有一个操作将调用这三个操作并聚合结果(如果可能的话),因为我对 Rails 架构不太确定。

希望这有帮助。

Well Any day I would suggest having a single call instead of 3 separate calls. As you mentioned you have a cost of making three different requests also the other reason for the same is the restriction on the number of concurrent connections to a domain on certain browsers (2 for IE7). Also think about your webserver and the maximum requests it can handle as the number of users increase.

Also you can still make it modular by having all the three actions and have an action that will call these three actions and aggregate the results if that is even possible as I am not too sure about the Rails architecture.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文