Skip to content Skip to sidebar Skip to footer

Firebase Off Removes All Callbacks For All EventTypes

When I call off on a reference for a specific eventType, firebase seems to be removing all callbacks for all event types. In the code below, I want to remove all callbacks for the

Solution 1:

    <script src="https://cdn.firebase.com/js/client/2.3.1/firebase.js"></script>
    <script>
    var root_ref = new Firebase('https://jcatest.firebaseio.com');

    var refChidAdded = root_ref.on('child_added', function(snap){
      console.log('child:', snap.key(), snap.val());
    });

    //change here
    var refChidRemoved = root_ref.on('child_removed', function(snap){
      console.log('child:', snap.key(), snap.val());
    });
    refChidRemoved.off(); // OR root_ref.off('child_removed', refChidRemoved );

    root_ref.child('foo_key').set('foo_val');
    </script>

Please call off() from the reference created by on() event.

Also, possible by removing maintenance of extra variables, do this directly :

 root_ref.on('child_removed').off()

Post a Comment for "Firebase Off Removes All Callbacks For All EventTypes"