Is there a way to know in what context Child Added is called? Particularly page load vs. other events

后端 未结 3 1187
故里飘歌
故里飘歌 2021-01-21 12:57

Here\'s my scenario. When child is added via a browser event post page load, I want to indicate it in the title. But on page load, child added is called as well.

How can

相关标签:
3条回答
  • 2021-01-21 13:23

    Firebase very intentionally does not distinguish "initial" data from "new" data. This allows for much simpler development in most cases, because you only have to write one set of logic for your data, rather than handling both the initial data case and the new data case.

    I can see how you would want the distinction in this case. I'm not sure exactly what you're doing, but if you're building a chat application, you might want to flash the title based on the timestamp of the most recent message rather than whether or not it's a "new" message. This would allow the title to flash on page load if a message was sent slightly before the page was loaded, which may be desirable. In some other cases, you may actually want to flash the title for unread data, and you may want to consider marking children as "read" and flashing the title only for children that show up without the "read" bit. This would allow things to work seamlessly across page refreshes.

    If you absolutely need to know when "new" data shows up, you could try using "once" with a "value" event type to get the data, and then use "on" with a startAt query and a "child_added" event type to display new data after that. It would look something like this:

    var data = new Firebase(...);
    data.once("value", function(d) {
      //TODO: display initial state...
    
      data.startAt(null, <last id in snapshot>).on("child_added", function(newMessSnapshot) {
        //TODO: render new child and flash title bar.
      }
    }
    

    Or if you want to do it the really simple way, you could just set a timer so that the title won't flash for any messages received within the first N seconds of page load.

    Hope that helps!

    0 讨论(0)
  • 2021-01-21 13:30

    I thought I would update Kato's answer as Datasnapshot.name() and limit have now both been depreciated.

    I decided to make it a bit more simple as well ;)

      // connect to firebase
      var fb = new Firebase("https://your-firebase-ref/");
      var firstTime = false;
    
      fb.limitToLast(1).on('child_added', function(snapshot) {
        if (firstTime) {
          console.log(snapshot.key(), snapshot.val());
        };
        firstTime = true;
      });

    I hope this helps

    0 讨论(0)
  • 2021-01-21 13:34

    You can set up a call that only receives new events rather than ones already existing using the approach from this SO question.

    So basically, call .on('child_added'...) with endAt and limit. You still get one entry (the last one), which you can just ignore:

    // connect to firebase
    var fb = new Firebase(...);
    
    // retrieve the last record from `ref` (endAt() tells it to start at the end)
    var first = true;
    fb.child('table_name').endAt().limit(1).on('child_added', function(snapshot) {
    
       if( first ) {
           // ignore the first snapshot, which is an existing record
           first = false;
           return;
       }
    
       // all records after the last continue to invoke this function
       console.log(snapshot.name(), snapshot.val());
    
    });
    
    0 讨论(0)
提交回复
热议问题