2014-10-08 16 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

顯然刪除路徑完全導致所有回調被取消。此行爲的解決方法是一次刪除一個孩子,而不是刪除他們的父路徑。