Firebase - limitToLast(5),然后加载以前的数据

时间:2015-12-09 02:18:45

标签: angularjs firebase

我有一个聊天程序。当应用程序加载时,我通过执行以下操作加载最后5条消息:

var chatRef = new Firebase("https://xxxx.firebaseio.com/chat/");
  $scope.query = chatRef.orderByChild("Datestamp").limitToLast(5);
  $scope.chatList = $firebaseArray($scope.query);

我想让用户选择加载更多以前的消息。但我不知道如何加载接下来的5条消息。我正在尝试这样的事情,但没有成功:

$firebaseArray(chatRef.orderByChild("Datestamp").endAt($scope.chatList[0].Datestamp).limitToFirst(5)).$loaded(function (data) {
  data.forEach(function (d) {
    $scope.chatList.push(d);
  });
});

基本上我的数据集如下所示:

Msg 1

Msg 2

Msg 3

Msg 4

Msg 5

Msg 6

Msg 7

Msg 8

Msg 9

Msg 10。

当我加载应用程序时,我说limitToLast(5),它会拉:

Msg 6

Msg 7

Msg 8

Msg 9

Msg 10

现在我想加载之前的2条记录,

Msg 4

Msg 5

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

您正在寻找import javax.swing.*; import java.awt.event.*; import java.awt.Color; import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.border.LineBorder; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultHighlighter; import javax.swing.text.Highlighter; public class Example { JTextPane textPane; public Example() { JFrame frame = new JFrame(); textPane = new JTextPane(); textPane.setText("are warehouse are arearea are rare"); hightlightAll("are"); frame.setContentPane(textPane); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private void hightlightAll(String word) { Highlighter highlighter = textPane.getHighlighter(); String text = textPane.getText(); String[] words = text.split("[\\p{Punct}\\s]+"); int lastIndex = 0; for (String str : words) { lastIndex = text.indexOf(str, lastIndex); int endIndex = lastIndex + str.length(); if (str.equals(word)) { try { highlighter.addHighlight(lastIndex, endIndex, DefaultHighlighter.DefaultPainter); } catch (BadLocationException e) { } } lastIndex = endIndex; } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Example(); } }); } } 。您需要传入您获得的第一条消息的值,因此在您的情况下传递消息6的时间戳。

endAt()

您将在结果中收到消息6,您已经显示了该消息。因此,您需要跳过结果中的最后一条消息。