简体   繁体   English

两个方向无限滚动与 Firebase/Firestore 后端

[英]Two direction infinity scroll with Firebase/Firestore backend

I would like to implement a two direction infinity scrolling using Firestore as backend and Vue.js as frontend.我想使用 Firestore 作为后端和 Vue.js 作为前端来实现双向无限滚动。 I don't want to load the full list from the beginning because the list could be huge !我不想从头加载完整列表,因为列表可能很大 If my approach is generally bad, please let me know what is the good one.如果我的方法通常很糟糕,请告诉我什么是好的。

I have the following code (just the relevant part) working mostly well:我有以下代码(只是相关部分)运行良好:

  up() {
    console.log("up()")
    let first = this.items[0]
    console.log("first: ", first.text, first.timestamp)
    db.collection("questions").orderBy("timestamp", "desc").endAt(first.timestamp).limit(this.itemsPerPage).onSnapshot((snapshot) => {
        this.updateItems(snapshot)
    })
  },
  down() {
    console.log("down()")
    let last = this.items[this.items.length - 1]
    console.log("last: ", last.text, last.timestamp)
    db.collection("questions").orderBy("timestamp", "desc").startAt(last.timestamp).limit(this.itemsPerPage).onSnapshot((snapshot) => {
        this.updateItems(snapshot)
    })
  },
  updateItems(snapshot) {
    console.log("updateItems()")
    const temp = []
    snapshot.forEach((item) => {
      temp.push(item.data())
    })
    this.items = temp
  },
  firstLoad() {
    db.collection("questions").orderBy("timestamp", "desc").limit(this.itemsPerPage).get().then((snapshot) => {
      this.updateItems(snapshot)
    })
  }

and here is the relevant part of the view:这是视图的相关部分:

<p>
{{items}}
</p>
<p>
  <a href="#" @click.prevent="up">UP</a>
</p>
<ul v-for="(item, index) in items">
  <li :key="index">#{{index}} {{item}}</li>
</ul>
<p>
  <a href="#" @click.prevent="down">DOWN</a>
</p>

but I cannot "move up" by more than one item, because of the limit() method always limiting the result set from the beginning.但我不能“向上移动”多个项目,因为 limit() 方法总是从一开始就限制结果集。 Is there any way to "invert" the limit() method when using endAt()?使用 endAt() 时有什么方法可以“反转”limit() 方法? Here is an anim gif, how it's working now:这是一个动画 gif,它现在是如何工作的: 在此处输入图片说明

Unfortunately the anim gif is not visible, so here is the video: https://youtu.be/1AR44J92Yg4不幸的是动画 gif 不可见,所以这里是视频: https : //youtu.be/1AR44J92Yg4

There unfortunately is no API to get a fixed number of items near the end of a range in Cloud Firestore.不幸的是,没有 API 可以在 Cloud Firestore 中的范围末尾附近获取固定数量的项目。 Firebase's realtime database had a limitToLast for that purpose, but that API doesn't exist on Firestore.为此,Firebase 的实时数据库有一个limitToLast ,但 Firestore 上不存在该 API。

You'll have to create a query with the reverse sort order, and then do a regular limit on that.您必须使用反向排序顺序创建一个查询,然后对其进行常规limit

db.collection("questions").orderBy("timestamp").startAt(first.timestamp).limit(this.itemsPerPage)

This means that you'll get the results from that query in the reverse order of what you want, so you'll have to invert how you add them to the UI too.这意味着您将从该查询中以您想要的相反顺序获得结果,因此您也必须反转将它们添加到 UI 的方式。

All of this is rather awkward for this specific use-case, so I'd recommend filing a feature request for getting a limitToLast method or reverse method on a query.对于这个特定用例,所有这些都相当尴尬,因此我建议提交功能请求以获取查询的limitToLast方法或reverse方法。

Just in case you're interested, this was my testbed for these queries: https://jsbin.com/dimuvu/edit?js,output以防万一你感兴趣,这是我对这些查询的测试平台: https ://jsbin.com/dimuvu/edit?js, output

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

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