简体   繁体   中英

Removing references to child_added in Firebase data

I'm writing a Firebase web app (Javascript). If I write a reference to child_added like this:

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

"auction" and "lotno" are variables and will change as the app runs. I am writing the script to close each old reference (using OFF) whenever this happens, and then I open a new reference. My OFF looks like this:

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?

Meaning, can I simply write Javascript that will close all child_added within 'bids'? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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