如何判断哪个后代用on(" child_changed")更改 [英] How to tell which descendants are changed with on("child_changed")

查看:93
本文介绍了如何判断哪个后代用on(" child_changed")更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有以下数据库结构:

  / 
+ users
+ 1
+物品
+ -xxx:hello
+ 2
+物品

然后;

  var usersRef =新的Firebase(https://mydb.firebaseio.com/用户); 
usersRef.on(child_changed,function(snapshot){
utils.debug(JSON.stringify(snapshot.exportVal()));
});

如果一个值world被推送到/ users / 1 / items可能会得到:

  {items:{-xxx:hello,-yyy:world那么,如何判断哪一个是变化的呢?






$ b

我需要在(child_added)每一个参考/用户/ $ id /物品?

注意:我试图在node.js中写入一个管理进程。

解决方案

child_changed事件只提供关于哪个直接子节点已经改变的信息。如果数据结构中更深的节点发生更改,您将知道哪个直接子节点受到影响,但不知道更改的数据的完整路径。这是设计的。



如果您想精确更新有关更改的内容,则应该递归地将回调附加到您关心的所有元素。通过这种方式,当物品发生变化时,您将知道物品是由哪个回调触发的。 Firebase实际上是针对此用例进行了优化的;附加大量的回调 - 甚至数千 - 应该可以正常工作。在幕后,Firebase将所有回调聚合在一起,只同步所需的最小数据集。

因此,对于您的示例,如果您希望每次都获得警报新的项目被添加到任何用户,你可以做到以下几点:

  var usersRef = new Firebase(https:// mydb .firebaseio.com /用户); 
usersRef.on(child_added,function(userSnapshot){
userSnapshot.ref()。child(items)。on(child_added,function(itemSnapshot)$ b $ utils。 debug(itemSnapshot.val());
});
});

如果您正在处理大量的用户(数十万或数百万)所有的数据是不切实际的,还有另一种方法。而不是让你的服务器直接监听所有的数据,你可以让它监听一系列的变化。然后,当客户端添加项目到他们的项目列表,他们也可以添加一个项目到这个队列,以便服务器知道它。



这是什么客户端代码可能看起来像:

  var itemRef = new Firebase(https://mydb.firebaseio.com/users/MYID/items ); 
var serverEventQueue =新的Firebase(https://mydb.firebaseio.com/serverEvents);
itemRef.push(newItem);
serverEventQueue.push(newItem);

然后,您可以让服务器侦听该队列上的child_added并处理事件。

For example, I have following database structure:

/ 
+ users
  + 1
    + items
      + -xxx: "hello"
  + 2
    + items

Then;

var usersRef = new Firebase("https://mydb.firebaseio.com/users");
usersRef.on("child_changed", function(snapshot) {
  utils.debug(JSON.stringify(snapshot.exportVal()));
});

If a value, "world", is pushed to "/users/1/items", I may get:

{"items": {"-xxx": "hello", "-yyy": "world"}}

So, how to tell which one is changed?

I need to on("child_added") every single ref to "/users/$id/items"?

NOTE: I'm trying to write an admin process in node.js.

解决方案

The child_changed event only provides information on which immediate child has changed. If a node deeper in a data structure changed, you'll know which immediate child was affected but not the full path to the changed data. This is by design.

If you want granular updates about exactly what changed, you should attach callbacks recursively to all of the elements you care about. That way when an item changes, you'll know what the item was by which callback is triggered. Firebase is actually optimized for this use case; attaching large numbers of callbacks -- even thousands -- should work fine. Behind the scenes, Firebase aggregates all of callbacks together and only synchronizes the minimum set of data needed.

So, for your example, if you want to get alerted every time a new item is added for any user, you could do the following:

var usersRef = new Firebase("https://mydb.firebaseio.com/users");
usersRef.on("child_added", function(userSnapshot) {
  userSnapshot.ref().child("items").on("child_added", function(itemSnapshot) 
    utils.debug(itemSnapshot.val());
  });
});

If you are working with a very large number of users (hundreds of thousands or millions), and synchronizing all of the data is impractical, there's another approach. Rather than have your server listen to all of the data directly, you could have it listen to a queue of changes. Then when clients add items to their item lists, they could also add an item to this queue so that the server becomes aware of it.

This is what the client code might look like:

var itemRef = new Firebase("https://mydb.firebaseio.com/users/MYID/items");
var serverEventQueue = new Firebase("https://mydb.firebaseio.com/serverEvents");
itemRef.push(newItem);
serverEventQueue.push(newItem);

You could then have the server listen for child_added on that queue and handle the events when they come in.

这篇关于如何判断哪个后代用on(" child_changed")更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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