2014-10-08 14 views
1

我正在使用Firebase-util的相交功能来查找给定链接的所有评论。这在我第一次调用联接时似乎工作的很好,但是当我删除数据库的内容并重新替换它们时,它似乎没有正确地通知我的值回调。只要资源路径保持不变,引用是否应该继续工作?使用Firebase-util连接表格时未正确触发Firebase事件

运行此示例。当您单击该按钮时,它将删除并重新创建数据。如您所见,数据重新创建后,注释列表不会重新填充。

<link rel="import" href="https://www.polymer-project.org/components/polymer/polymer.html"> 

<script src="http://cdn.firebase.com/v0/firebase.js"></script> 
<script src="https://cdn.firebase.com/libs/firebase-util/0.1.0/firebase-util.min.js"></script> 

<polymer-element name="my-element"> 
    <template> 
    <h1>Test of Firebase-util.intersection</h1> 
    <div> 
     <button on-click={{initializeFirebase}}>Reset data</button> 
    </div> 
    <ul> 
     <template repeat="{{rootComment in comments}}"> 
     <li>{{rootComment.comment.content}} 
      <ul> 
      <template repeat="{{subComment in rootComment.children}}"> 
       <li>{{subComment.comment.content}} 
       <ul> 
        <template repeat="{{subSubComment in subComment.children}}"> 
        <li>{{subSubComment.comment.content}}</li> 
        </template> 
       </ul> 
       </li> 
      </template> 
      </ul> 
     </li> 
     </template> 
    </ul> 
    </template> 
    <script> 
    Polymer('my-element', { 
     ready: function() { 
     var sanitizeUrl = function(url) { 
      return encodeURIComponent(url).replace(/\./g, '%ZZ'); 
     }; 
     var baseUrl = "https://nested-comments-test.firebaseio.com"; 
     var linkUrl = baseUrl + 
      '/links/' + 
      sanitizeUrl(document.URL) + 
      '/comments'; 
     var commentsUrl = baseUrl + '/comments'; 
     var root = new Firebase(baseUrl); 

     this.initializeFirebase = function() { 
      function addLink(url, callback) { 
      var key = sanitizeUrl(url), 
       newLink = { 
       url: url, 
       createdAt: Firebase.ServerValue.TIMESTAMP 
       }; 
      root.child('/links/' + key).update(newLink); 
      callback(key); 
      } 

      function addComment(attributes, callback) { 
      return root.child('/comments').push(attributes, callback); 
      } 

      function onCommentAdded(childSnapshot) { 
      var newCommentId = childSnapshot.name(), 
       attributes = {}, 
       link = childSnapshot.val().link, 
       url = '/links/' + link + '/comments'; 
      attributes[newCommentId] = true; 
      root.child(url).update(attributes); 
      } 

      root.remove(function() { 
      root.child('/comments').on('child_added', onCommentAdded); 
      addLink(document.URL, function(link) { 
       var attributes = { 
        link: link, 
        content: "This is the first comment." 
       }, 
       firstCommentId, secondCommentId; 
       firstCommentId = addComment(attributes).name(); 
       attributes = { 
       link: link, 
       content: "This is a reply to the first.", 
       replyToCommentId: firstCommentId 
       }; 
       secondCommentId = addComment(attributes).name(); 
       attributes = { 
       link: link, 
       content: "This is a reply to the second.", 
       replyToCommentId: secondCommentId 
       }; 
       addComment(attributes); 
       attributes = { 
       link: link, 
       content: "This is another reply to the first.", 
       replyToCommentId: firstCommentId 
       }; 
       addComment(attributes); 
      }); 
      }); 
     }; 
     this.initializeFirebase(); 

     var findChildrenForComment = function(snapshot, parentCommentId) { 
      var returnVal = []; 
      snapshot.forEach(function(snap) { 
      var comment = snap.val(), 
       commentId = snap.name(); 
      if (comment.replyToCommentId === parentCommentId) { 
       var children = findChildrenForComment(snapshot, commentId); 
       var obj = { 
       commentId: commentId, 
       comment: comment, 
       parentId: parentCommentId 
       }; 
       if (children.length) { 
       obj.children = children; 
       } 
       returnVal.push(obj); 
      } 
      }); 
      return returnVal; 
     }; 

     this.ref = Firebase.util.intersection(
      new Firebase(linkUrl), 
      new Firebase(commentsUrl) 
     ); 
     this.comments = {}; 
     var that = this; 
     this.ref.on('value', function(snapshot) { 
      that.comments = findChildrenForComment(snapshot); 
     }); 
     } 
    }); 
    </script> 
</polymer-element> 
<my-element></my-element> 
+0

这可能与加入无关,并且正在发生,因为ref指向的位置已被删除。不过,我认为如果Firebase参考资料被移除并重新添加,它应该继续工作。 – cayblood 2014-10-08 20:18:19

+0

我现在已经确认,只要我在添加新评论前不删除最后的评论,我的ref回调继续正常工作。 – cayblood 2014-10-08 20:31:22

回答

0

显然删除路径完全导致所有回调被取消。此行为的解决方法是一次删除一个孩子,而不是删除他们的父路径。