有什么方法可以知道在什么情况下添加了儿童?特别是页面加载与其他事件 [英] Is there a way to know in what context Child Added is called? Particularly page load vs. other events

查看:100
本文介绍了有什么方法可以知道在什么情况下添加了儿童?特别是页面加载与其他事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景。当通过浏览器事件发布页面加载添加子项时,我想在标题中指明它。但是在页面加载时,也会调用子项。



如何区分添加的初始子项和实际添加了新的项目的子项。



Thanks,Tim

解决方案

Firebase非常有意无法区分初始数据新数据。这在大多数情况下允许更简单的开发,因为您只需为数据编写一组逻辑,而不是同时处理初始数据和新数据。

我可以看到你在这种情况下会如何区分。我不确定你在做什么,但是如果你正在构建一个聊天应用程序,你可能想要根据最新消息的时间戳来标题,而不是它是否是新消息。如果在加载页面之前稍微发送了一条消息,这将允许标题在页面加载时闪烁,这可能是可取的。在其他一些情况下,您可能实际上需要为未读数据闪烁标题,并且您可能需要考虑将子标记为已读,并且仅针对未显示已读位的子标题闪烁标题。如果你确实需要知道什么时候出现新数据,你可以尝试使用一次和一次值事件类型来获取数据,然后使用startAt查询和child_added事件类型的on来显示新的数据。它看起来像这样:

  var data = new Firebase(...); 
data.once(value,function(d){
// TODO:显示初始状态...

data.startAt(null,< last id in快照>)on(child_added,function(newMessSnapshot){
// TODO:render new child and flash title bar。
}
}


希望有帮助!


Here's my scenario. When child is added via a browser event post page load, I want to indicate it in the title. But on page load, child added is called as well.

How can I differentiate the initial child added vs one where a new entry has been actually added.

Thanks, Tim

解决方案

Firebase very intentionally does not distinguish "initial" data from "new" data. This allows for much simpler development in most cases, because you only have to write one set of logic for your data, rather than handling both the initial data case and the new data case.

I can see how you would want the distinction in this case. I'm not sure exactly what you're doing, but if you're building a chat application, you might want to flash the title based on the timestamp of the most recent message rather than whether or not it's a "new" message. This would allow the title to flash on page load if a message was sent slightly before the page was loaded, which may be desirable. In some other cases, you may actually want to flash the title for unread data, and you may want to consider marking children as "read" and flashing the title only for children that show up without the "read" bit. This would allow things to work seamlessly across page refreshes.

If you absolutely need to know when "new" data shows up, you could try using "once" with a "value" event type to get the data, and then use "on" with a startAt query and a "child_added" event type to display new data after that. It would look something like this:

var data = new Firebase(...);
data.once("value", function(d) {
  //TODO: display initial state...

  data.startAt(null, <last id in snapshot>).on("child_added", function(newMessSnapshot) {
    //TODO: render new child and flash title bar.
  }
}

Or if you want to do it the really simple way, you could just set a timer so that the title won't flash for any messages received within the first N seconds of page load.

Hope that helps!

这篇关于有什么方法可以知道在什么情况下添加了儿童?特别是页面加载与其他事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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