1

I have an view that has a for loop that inserts rows to a table. The table is very big and already consisting of couple of thousand of rows.

When I run it, the server throws out of memory exception.

I would like to add an infinite scrolling feature so I won't have to load all the data at once.

Right now the data is being sent with regular res.render(index.ejs, data) (data is JSON)

I can figure out the infinite scrolling part, but how do I get the JSON data in chunks from the server ?

I am using node.js with express and ejs as template engine. I am open for using any framework that will aid me through the process (was particularly checking out Angualr.js).

Thanks

4

1 回答 1

2

首先,有一个无限滚动的角度组件:http: //ngmodules.org/modules/ngInfiniteScroll

然后,您必须将后端查询更改为如下所示:

http://my.domain.com/items?before=1392382383&count=50

这实际上告诉您的服务器获取在给定时间戳之前创建/发布/更改/任何内容的项目,并仅返回其中的 50 个。这意味着您的后端实体(例如博客条目、产品等)需要在连续空间中缺少自然排序(发布日期时间戳几乎是连续的)。这非常重要,因为即使您使用时间戳,您最终也可能会出现极端的 heisenbugs,其中项目被渲染两次(如果您使用 <= 那是确定的),松散的条目(如果您使用 < 和边缘的 2 个条目)您的结果集在同一个时间戳上)甚至一次又一次地加载相同的项目(超过 50 个项目在同一个时间戳上)。您必须通过过滤重复来处理此类情况。

您的服务器端代码将其转换为类似的查询(当然是 DB2 SQL):

SELECT * FROM ITEMS
WHERE PUBLICATION_DATE <= 1392382383
ORDER BY PUBLICATION_DATE DESC
FETCH FIRST 50 ROWS ONLY

当无限滚动到达页面末尾并调用您注册的回调时,您会$http.get通过考虑已加载项目的最后一项来创建此请求。对于第一个查询,您可以使用当前时间戳。

另一种方法是简单地发送最后一项的 id,例如:

http://my.domain.com/items?after_item=1232&count=50

让服务器决定做什么。我想你可以使用像 Redis 这样的 NoSQL 存储来非常快速地回答这种查询并且没有副作用。

这是一般的想法。我希望它有所帮助。

于 2013-11-05T15:54:57.963 回答