简体   繁体   English

从 Lumen 加载少量数据 API 时间

[英]Loading small amounts of data from Lumen API time

I have an API built in lumen and I plan to consume the API json response in the frontend using a single page framework like Angular.我有一个内置流明的 API,我计划在前端使用 ZC3DE1C338BADD378 之类的单页框架使用 API json 响应

The problem is the response from some routes contain huge amount of data which is aprox 50000 rows as of now.问题是某些路由的响应包含大量数据,截至目前大约有 50000 行。 I intend to send the data to the frontend in small portion depending on how much the user is using.我打算根据用户的使用量将数据一小部分发送到前端。

for example on a social site you get small chunks of posts loaded at a time and as you scroll down more post are loaded until you eventual close the application例如,在社交网站上,您一次加载一小部分帖子,当您向下滚动时,会加载更多帖子,直到您最终关闭应用程序

I would like my API to do something similar.我希望我的 API 做类似的事情。 Alternatives that I have considered are我考虑过的替代方案是

  1. Using the Laravel Chunk method.使用 Laravel方法。 The problem with this method I have found that chunking does not necessarily bring small amounts of data at time, it just sub divides it and I can't use it because the server will load for a very long time if the data to be subdivided is huge as in my case and will eventually run out of memory and it is bad UX.这个方法的问题我发现分块不一定会带来少量的数据,它只是细分它,我不能使用它,因为如果要细分的数据是服务器会加载很长时间在我的情况下很大,最终会用完 memory ,这是糟糕的用户体验。
  2. Using the Laravel Paginate method.使用 Laravel分页方法。 Even though this does exactly what I described above, the issue I have is that I want the data to be in list format and lazyloaded whenever a user reaches the end of a page exactly the way facebook does when you reach the last post.尽管这与我上面描述的完全一样,但我遇到的问题是,我希望数据采用列表格式并在用户到达页面末尾时延迟加载,就像 facebook 在您到达最后一篇文章时所做的那样。 I don't want users to paginate to view more data.我不希望用户分页查看更多数据。 I would like the API to keep sending data depending on where it left.我希望 API 根据其离开的位置继续发送数据。

What options do I have for achieving this and also is there a workaround on the options I mentioned above?我有什么选择来实现这一点,还有我上面提到的选项的解决方法吗?

THANK YOU谢谢你

So, since the Paginate method does exactly what you need , you will need to use it.因此,由于 Paginate 方法完全符合您的需要,因此您将需要使用它。 Implement an API function that will receive a starting point and an offset on the server-side.实现一个 API function,它将在服务器端接收一个起点和一个偏移量。

On the client-side you do not have to display paging controls.在客户端,您不必显示分页控件。 You can instead implement a scroll event and detect if the scrolling reached the bottom .您可以改为实现scroll事件并检测滚动是否到达底部 If so, then trigger a function (still in JS on the client-side), that will ask for the next packet of data.如果是这样,则触发function (仍在客户端的 JS 中),它将请求下一个数据包。 For this purpose, you will always need to know/find out how many records were already loaded and send that number as the starting point and your offset to the API function we have discussed above.为此,您总是需要知道/找出已经加载了多少记录,并将该数字作为起点和您的偏移量发送到我们上面讨论过的 API function

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

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