简体   繁体   English

删除对Firebase数据中child_added的引用

[英]Removing references to child_added in Firebase data

I'm writing a Firebase web app (Javascript). 我正在编写Firebase网络应用程序(Javascript)。 If I write a reference to child_added like this: 如果我这样写对child_added的引用:

dataRef.child('bids').child(auction).child(lotno).on('child_added', function(data){...}

"auction" and "lotno" are variables and will change as the app runs. “拍卖”和“ lotno”是变量,会随着应用程序的运行而改变。 I am writing the script to close each old reference (using OFF) whenever this happens, and then I open a new reference. 每当发生这种情况时,我都会编写脚本以关闭每个旧引用(使用OFF),然后打开一个新引用。 My OFF looks like this: 我的OFF看起来像这样:

dataRef.child('bids').child(auction).child(lotno).off('child_added');

First of all, am I doing this correctly? 首先,我这样做正确吗? And second, is there a more universal way I can close child_added references, to avoid the possibility of one being left open accidentally? 其次,是否有一种更通用的方式可以关闭添加了child_add的引用,以避免意外打开一个引用的可能性?

Meaning, can I simply write Javascript that will close all child_added within 'bids'? 意思是,我可以简单地编写将关闭“出价”中所有child_added的JavaScript吗? Wondering if something like this is valid: 想知道这样的事情是否有效:

dataRef.child('bids').off('child_added');

Is there a wildcase that would cover all children? 是否有一个可以覆盖所有儿童的野箱? Would that close everything, or must each specific path through child elements be written out? 是否会关闭所有内容,还是必须写出通过子元素的每个特定路径?

Since the path is built with two variables, and only one listener is open at any given time, I decided to solve this problem by simply storing the two values separately, in their own variables, such as: 由于路径是用两个变量构建的,并且在任何给定时间仅打开一个侦听器,因此我决定通过简单地将两个值分别存储在它们自己的变量中来解决此问题,例如:

    var storedPath1;
    var storedPath2;

    dataRef.child('bids').child(auction).child(lotno).on('child_added', function(data){
        if (storedPath1!=undefined && storedPath2!=undefined{
            dataRef.child('bids').child(storedPath1).child(storedPath2).off('child_added');
        }
        storedPath1=auction;
        storedPath2=lotno;
    }

If the listener was previously established, this will close it when opening the new one. 如果先前已建立了侦听器,则在打开新侦听器时将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM