首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Firebase-util连接表时未正确触发的火基事件

使用Firebase-util连接表时未正确触发的火基事件
EN

Stack Overflow用户
提问于 2014-10-08 19:03:21
回答 1查看 163关注 0票数 1

我使用Firebase-util的交集函数来查找给定链接的所有注释。这在我第一次调用join时似乎运行良好,但当我删除数据库的内容并再次替换它们时,它似乎没有正确地通知我的值回调。只要资源路径保持不变,引用就不应该继续工作吗?

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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-03 21:49:05

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26264502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档