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

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

问题描述

我试着测试Firebase以允许用户使用 push 发表评论。我想显示我检索到的数据,如下所示:

$ $ $ $ $ $ $ c $ fbl.child('sell').limit(20)。 on(value,function(fbdata){
//在这里处理数据显示
}

问题是数据是按照从最旧到最新的顺序返回的 - 我想要以相反的顺序。Firebase可以这样做吗?

解决方案<由于这个答案是写的,Firebase已经添加了一个功能,允许任何孩子或价值排序,所以现在有四种方法来订购数据:按键,按价值,按优先级或任何已命名的孩子的价值。请参阅介绍新订购功能的博客文章。



基本方法保持不变: em>



1.添加一个带有倒置时间戳的子属性,然后对其进行排序。

2。


Firebase支持检索集合中的子节点。有两种方法:
$ b $ ul

  • 按名称

  • 按优先级

  • ul>

    你现在得到的是名字,这恰好是按时间顺序排列的。这不是巧合:当你把一个项目推入一个集合中时,会生成这个名字,以确保这些子项是以这种方式排序的。引用 Firebase文档< push $ b


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

    有关订购数据的Firebase指南对此话题有这样的说明:


    数据如何排序



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

    获取行为的最简单方法你想要的也是指定一个总是递减的优先级,当你添加项目:

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



    更新



    以不同的方式检索孩子:

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

    在我的测试中,使用('child_added')确保最后添加的几个孩子以相反的时间顺序返回。 c> on('value'另一方面,按照名称的顺序返回它们。



    请务必阅读阅读有序数据部分,解释了使用 child_ * 事件来检索(排序)的子项。



    一个bin来演示这个: a href =http://jsbin.com/nonawe/3/watch?js,console =nofollow noreferrer> http://jsbin.com/nonawe/3/watch?js,console


    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
    }
    

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

    解决方案

    Since this answer was written, Firebase has added a feature that allows ordering by any child or by value. So there are now four ways to order data: by key, by value, by priority, or by the value of any named child. See this blog post that introduces the new ordering capabilities.

    The basic approaches remain the same though:

    1. Add a child property with the inverted timestamp and then order on that.

    2. Read the children in ascending order and then invert them on the client.

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

    • by name
    • by priority

    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:

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

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

    How Data is Ordered

    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().limit(20).on('child_added', function(fbdata) {
      console.log(fbdata.exportVal());
    })
    

    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.

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

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

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

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