将侦听器添加到Firebase生成的密钥中的一个孩子? [英] Add a listener to a child inside of a firebase generated key?

查看:106
本文介绍了将侦听器添加到Firebase生成的密钥中的一个孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定布局

  + activity 
+ -K5p_pBXog4kb0SVLXxj
text:'something',
当:111
+线程
+ -K5paF53zm3cuudP9FUQ
id:8,
text:'别的东西'

有没有办法将一个监听器添加到像/ activity / * / thread这样的活动中的任何新的线程中?

添加新活动时,我得到这些饲料,但是当一个线程被追加没有什么触发器。我需要将[thread]部分移出到它自己的根目录,并将线程ID与活动ID进行匹配,以便知道何时对主活动线程进行线程评论?

解决方案

根据您的评论更新答案。



您要收听 / activity / $ theid / thread / $ threadId



不是嵌套,而是分解 / thread / $ threadId
$ {
threadActivity:{
$ activity $ b $ $ $ threadId $ {

$ b}


code $

现在,当添加一个新线程时,您可以只听$ / threadActivity / $ activityId

  var ref = new Firebase('https://< my> .firebaseio.com / threadActivity'); 
var activityRef = ref.child('some-id');
activityRef.on('child_added',function(snap){
//将会更新你所在位置的每个孩子,并且每个孩子都被添加
console.log(snap.val()) ;
});


Given the layout

+ activity
  + -K5p_pBXog4kb0SVLXxj
    text:'something',
    when:111
    + thread
      + -K5paF53zm3cuudP9FUQ 
        id:8,
        text:'something else'

Is there a way to add a listener to any new "thread" inserts inside of activity like /activity/*/thread?

When new activities get added I am getting the feed on those but when a thread is appended nothing triggers. Do I need to move the [thread] section out to it's own root and match thread ID's with activity ID's to know when a thread comment is made against the main activity thread?

解决方案

Updating the answer based upon your comment.

You want to listen to /activity/$theid/thread/$threadId:

Rather than nesting, break out the /thread/$threadId into it's own location at the root.

{
  "threadActivity": {
     "$activityId": {
        "$threadId" : {

        } 
     }
   } 
}

Now when a new thread is added you can just listen to /threadActivity/$activityId:

var ref = new Firebase('https://<my>.firebaseio.com/threadActivity');
var activityRef = ref.child('some-id');
activityRef.on('child_added', function(snap) {
  // will update you for every child at the location and each child added
  console.log(snap.val()); 
});

这篇关于将侦听器添加到Firebase生成的密钥中的一个孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆