以降序显示帖子 [英] Display posts in descending posted order

查看:29
本文介绍了以降序显示帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试 Firebase 以允许用户使用 push 发表评论.我想用以下内容显示我检索到的数据;

I'm trying to test out Firebase to allow users to post comments using push. I want to display the data I retrieve with the following;

fbl.child('sell').limit(20).on("value", function(fbdata) { 
  // handle data display here
}

问题是数据是按从旧到新的顺序返回的 - 我希望以相反的顺序返回.Firebase 可以这样做吗?

The problem is the data is returned in order of oldest to newest - I want it in reversed order. Can Firebase do this?

推荐答案

自从撰写此答案以来,Firebase 添加了一项功能,允许按任何子项或按值进行排序.所以现在有四种排序数据的方法:按键、按值、按优先级或按任何命名子项的值.请参阅此博文,介绍新的订购功能.

基本方法保持不变:

1.添加具有反转时间戳的子属性,然后对其进行排序.

2.按升序读取子项,然后在客户端反转它们.

Firebase 支持通过两种方式检索集合的子节点:

Firebase supports retrieving child nodes of a collection in two ways:

  • 按名称
  • 按优先级

您现在得到的是按时间顺序排列的名称.顺便说一句,这并非巧合:当您 push 一个项目到一个集合中时,会生成名称以确保以这种方式对子项进行排序.引用 Firebase 文档 push:

What you're getting now is by name, which happens to be chronological. That's no coincidence btw: when you push an item into a collection, the name is generated to ensure the children are ordered in this way. To quote the Firebase documentation for push:

push() 生成的唯一名称以客户端生成的时间戳为前缀,以便结果列表按时间顺序排序.

The unique name generated by push() is prefixed with a client-generated timestamp so that the resulting list will be chronologically-sorted.

关于有序数据的 Firebase 指南 关于这个话题有这样的说法:

The Firebase guide on ordered data has this to say on the topic:

数据的排序方式

默认情况下,Firebase 节点的子节点按名称按字典顺序排序.使用 push() 可以生成自然按时间顺序排序的子名称,但许多应用程序需要以其他方式对其数据进行排序.Firebase 允许开发者通过为每个项目指定自定义优先级来指定列表中项目的顺序.

By default, children at a Firebase node are sorted lexicographically by name. Using push() can generate child names that naturally sort chronologically, but many applications require their data to be sorted in other ways. Firebase lets developers specify the ordering of items in a list by specifying a custom priority for each item.

获得所需行为的最简单方法是在添加项目时还指定始终降低的优先级:

The simplest way to get the behavior you want is to also specify an always-decreasing priority when you add the item:

var ref = new Firebase('https://your.firebaseio.com/sell');
var item = ref.push();
item.setWithPriority(yourObject, 0 - Date.now());

更新

您还必须以不同的方式检索子项:

Update

You'll also have to retrieve the children differently:

fbl.child('sell').startAt().limitToLast(20).on('child_added', function(fbdata) {
  console.log(fbdata.exportVal());
})

在我的测试中使用 on('child_ added' 确保最后添加的几个孩子按时间倒序返回.另一方面,使用 on('value', 按名称顺序返回.

In my test using on('child_added' ensures that the last few children added are returned in reverse chronological order. Using on('value' on the other hand, returns them in the order of their name.

请务必阅读部分读取有序数据",解释了 child_* 事件用于检索(有序)子项的用法.

Be sure to read the section "Reading ordered data", which explains the usage of the child_* events to retrieve (ordered) children.

一个展示这一点的 bin:http://jsbin.com/nonawe/3/看?js,控制台

A bin to demonstrate this: http://jsbin.com/nonawe/3/watch?js,console

这篇关于以降序显示帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆