取消链接父级监听器是否适用于Firebase中的孩子 [英] Unlink listener for parent does it applied to children in Firebase

查看:97
本文介绍了取消链接父级监听器是否适用于Firebase中的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase在实时应用程序中同步数据。经过一些处理,我想要取消所有添加的听众。所以,我把

  myRef.off(); 

但是我发现有些听众仍然附着在上面。

我的问题是:当你把监听器关闭一个父节点,它传播给孩子,或者我应该把每个级别分开吗?

解决方案



$ $ $ $ $ $ $ $ $ ref()value,function(snapshot){
console.log(parent:+ JSON.stringify(snapshot.val()));
}); $($)$ b $ ref.child(child)。(value,function(snapshot){
console.log(child:+ JSON.stringify(snapshot.val()));
});
ref.set('1');
ref.child('child')。set('2');
ref.off();
ref.child('child')。set('3');
ref.child('child')。off();
ref.set('4');

输出:

 父母:1
孩子:2
父母:{孩子:2}
孩子:3

所以在父监听器上调用 off 之后,子监听器仍然会触发( 3)。但是,如果我们得到同样的孩子,并调用 off ,它就不会再有了(4)。 / p>

JSBin: http://jsbin.com / wutaza / edit?js,console


$ b 结论:off()不会从子节点中删除侦听器。

I am using firebase to synch data in a real time app. After some treatment, I want to unlink all listeners added. So I put

myRef.off();

But I discover that some listeners are still attached.

My question is: when you put listeners to off for a parent node does it propagate to children or should I put off each level separately?

解决方案

Let's try it.

ref.on("value", function(snapshot) { 
  console.log("parent: "+JSON.stringify(snapshot.val()));
});
ref.child("child").on("value", function(snapshot) { 
  console.log("child: "+JSON.stringify(snapshot.val()));
});
ref.set('1');
ref.child('child').set('2');
ref.off();
ref.child('child').set('3');
ref.child('child').off();
ref.set('4');

The output:

parent: "1"
child: "2"
parent: {"child":"2"}
child: "3"

So after calling off on the parent listener, the child listener still fires ("3"). But if we get the same child and call off, it doesn't for anymore ("4").

JSBin: http://jsbin.com/wutaza/edit?js,console

Conclusion: off() doesn't remove listeners from child nodes.

这篇关于取消链接父级监听器是否适用于Firebase中的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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