admin 管理员组文章数量: 1086019
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?
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?
Share Improve this question asked Jun 16, 2015 at 18:50 iouhammiiouhammi 1,10815 silver badges29 bronze badges3 Answers
Reset to default 8Let'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./wutaza/edit?js,console
Conclusion: off() doesn't remove listeners from child nodes.
Detach a callback previously attached with on(). Note that if on() was called multiple times with the same eventType and callback, the callback will be called multiple times for each event, and off() must be called multiple times to remove the callback.
This es from the firebase docs on the off() method (link).
So if i read this correct you have to call off() for every on() you did.
Thanks to the posts in this thread, I was able to figure out how stop my callbacks which kept firing after their initial invocation for the intended Firebase calls they were passed to. This unexpected callback was even happening when I updated directly in the console.
I got the following solution to disengage listeners once the callback runs:
var queryRef = someItemRef.limitToLast(1);
queryRef.on('child_added', function (data) {
queryRef.off();
onAddSomeItemReadyFunc(data);
});
For me - understanding the listener logic and avoiding "rogue callbacks" - has been a trickier aspect of working with Firebase DB. But we're gettn' there :)
@Frank van Puffelen, thank you yet again for good notes and examples to help us Firebase-client devs get back on track!
本文标签: javascriptUnlink listener for parent does it applied to children in FirebaseStack Overflow
版权声明:本文标题:javascript - Unlink listener for parent does it applied to children in Firebase - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743991586a2514827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论