简体   繁体   English

在node.js Web应用程序中延迟加载MySQL查询

[英]Lazy Loading for MySQL queries in node.js web app

So I have a page that upon its get I pull some matching rows from the mysql database. 所以我有一个页面,在get页面时,我从mysql数据库中提取了一些匹配的行。 Currently I pull 8 as that's how many I want to show on each page. 目前我拉8,因为那是我希望在每个页面上显示的数量。 I wanted to implement something that facebook has which i've seen is known as Lazy Loading where when the user scrolls down more content will load, in this case additional sql rows that match the query. 我想实现Facebook具有的功能,我已经看到它被称为“延迟加载”,当用户向下滚动时,将加载更多内容,在这种情况下,将匹配查询的其他sql行。

I could just grab a whole lot and have jQuery only render eight at a time but lazy loading sounds much better. 我可以抓很多东西,让jQuery一次只渲染八个,但是延迟加载听起来要好得多。 Problem is I can't find anything on Lazy Loading with node apps using MySQL. 问题是我无法在使用MySQL的节点应用程序的延迟加载中找到任何内容。 Does anybody know of a tutorial, article or would like to post a detailed explanation of how to do this? 是否有人知道教程,文章或想发布有关如何执行此操作的详细说明? (I is a newb) (我是新手)

PS I have seen some post/server side interaction without leaving the page done with ajax but I know literally nothing about ajax, so if there is a way to do it just with javascript or jquery I'm all about that. PS我已经看到了一些发布/服务器端的交互,而没有用ajax完成页面,但是我对ajax几乎一无所知,因此,如果有一种方法可以仅使用javascript或jquery来实现,那么我就是这样。 Thanks for any response! 感谢您的任何回复!

The concept your are talking about is also called "paging", like when you see a table that displays 10 rows at a time, though really there are 87 rows and page 1 is 1-10, page 2 is 11-20, etc. 您正在谈论的概念也称为“分页”,例如当您看到一个表同时显示10行时,尽管实际上有87行,并且第1页是1-10,第2页是11-20,依此类推。

When doing lazy loading, your query would order by date (or whatever your criteria) and work something like this: 进行延迟加载时,查询将按日期(或其他条件)排序并按以下方式工作:

  1. Load the page with the query finding the first 10 items. 用查询加载页面,查找前10个项目。 LIMIT 0, 10
  2. Store currentPage=1 locally in your page's javascript, and what size or scroll limit is associated with "page1" - is it based on pixels? currentPage=1本地存储在页面的javascript中,与“ page1”关联的大小或滚动限制-它基于像素吗? Or items displayed? 或显示的项目?
  3. Detect when you scroll to the bottom of the page, or towards the bottom of the page (definitely matters when user scrolls back up!) 检测何时滚动到页面底部或滚动到页面底部(当用户向上滚动时绝对重要!)
  4. Initiate the lazy loading query. 启动延迟加载查询。

    1. First build the spinner on your page 首先在页面上构建微调器
    2. Make a call to your server for the query, with the same ordering, and this time LIMIT based on the last page number, currentPage*10 eg currentPage=3 , so LIMIT 30 40 让您的服务器的查询电话,以相同的顺序,这一次LIMIT基于最后一个页码, currentPage*10currentPage=3 ,所以LIMIT 30 40
    3. Add the results to your displayed array when the server responds. 服务器响应时,将结果添加到显示的阵列中。
    4. Increase the currentPage count, as associated scrolling limits 增加currentPage数量,作为相关的滚动限制
  5. Wait for user to scroll to next "bottom" and repeat steps 3 and 4. 等待用户滚动到下一个“底部”并重复步骤3和4。

That's a rough outline, and hopefully points you in the right direction! 这是一个粗略的轮廓,希望可以为您指明正确的方向!

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

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