如何只检索新数据? [英] How to retrieve only new data?

查看:64
本文介绍了如何只检索新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的站点管理员创建简单的通知系统,我只需要向每个管理员用户发送实时消息.但是当我使用 firebase 时,它​​会在每个页面上加载旧数据,并且用户会看到数据库中的所有消息.如果我设置了 limit(1) 用户将在每个页面重新加载时看到最后一个通知:

I'm trying to create simple notification system for my site admin, and I need to send only real-time messages to every admin user. But when I use firebase it loads old data on every page, and user see all messages from database. If I set limit(1) user will see last notification on every page reloading:

var eventsList = new Firebase('https://*****-messages.firebaseio.com/');

eventsList.on('child_added', function(message) {
    var message = message.val();
    $.notification(message.message);
});

如何只加载新消息,而不加载旧的通知历史记录?

How I can load only new messages, without old notification history?

推荐答案

这是设计使然,在实时系统中没有最新"数据的概念,因为它总是在变化.但是,如果您只想显示在页面加载之后添加到列表中的项目,您可以执行以下操作:

This is by design, in a real-time system there is no concept of the "latest" data because it's always changing. However, if you want to only display items added to the list after the page has loaded, you can do the following:

var newItems = false;
var eventsList = new Firebase('https://*****-messages.firebaseio.com/');

eventsList.on('child_added', function(message) {
  if (!newItems) return;
  var message = message.val();
  $.notification(message.message);
});
eventsList.once('value', function(messages) {
  newItems = true;
});

这篇关于如何只检索新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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