我试图跟踪某个路径在Firebase中包含多少个孩子。我一直在尝试使用on('child_added')和on('child_removed')回调来保持计数更新,但是即使现有的子代也可以调用它们。 Here是一个演示此问题的codepen。我还希望能够编写一个安全规则,以确保计数始终正确,但是似乎无法获得对象中子代的数量。

<script src="http://www.polymer-project.org/platform.js"></script>
<link rel="import" href="http://www.polymer-project.org/components/polymer/polymer.html">

<script src="https://cdn.firebase.com/js/client/1.1.2/firebase.js"></script>

<polymer-element name="my-element">
  <template>
    <div>
      <h1>Posts ({{count}})</h1>
      <template repeat="{{key in keys}}">
        <span>{{posts[key].content}} </span>
      </template></br>
      <button on-click="{{addPost}}">Add Post</button>
      <button on-click="{{removePost}}">Remove Post</button>
    </div>
  </template>
  <script>
    Polymer('my-element', {
      addPost: function () {
        var self = this;
        self.ref.child('posts').push({content: 'YO'});
      },
      removePost: function () {
        if (this.keys.length > 0) {
          var self = this;
          var topId = self.keys[0];
          self.ref.child('posts/' + topId).remove();
        }
      },
      ready: function () {
        var baseUrl = "https://transaction-test.firebaseio.com";
        var self = this;
        self.ref = new Firebase(baseUrl);
        self.ref.child('posts').on('value', function (snap) {
          self.posts = snap.val();
          self.keys = Object.keys(self.posts);
        });
        self.ref.child('postsCount').on('value', function (snap) {
          self.count = snap.val();
        });
        self.ref.child('posts').on('child_added', function (snap) {
          self.ref.child('postsCount').transaction(function (count) {
            return count + 1;
          });
        });
        self.ref.child('posts').on('child_removed', function (snap) {
          self.ref.child('postsCount').transaction(function (count) {
            return count - 1;
          });
        });
      }
    });
  </script>
</polymer-element>
<my-element></my-element>

最佳答案

child_addedchild_removed事件将为添加到客户端的每个子对象触发,因此将从服务器下载(或本地添加)的所有内容触发。

您最好保留一个单独的postcount是一个好主意。但是,应该从child_addedchild_removed触发它,而不是从addPostremovePost触发它。

像这样:

  addPost: function () {
    var self = this;
    self.ref.child('posts').push({content: 'YO'}, function(error) {
      if (!error) {
        self.ref.child('postsCount').transaction(function (count) {
          return count + 1;
        });
      }
    });
  },
  removePost: function () {
    if (this.keys.length > 0) {
      var self = this;
      var topId = self.keys[0];
      self.ref.child('posts/' + topId).remove(function(error) {
        if (!error) {
          self.ref.child('postsCount').transaction(function (count) {
            return count - 1;
          });
        }
      });
    }
  },


请注意,您的代码当前是方法的混合和匹配。如果您已经收到了所有帖子,则可以在此处简单地将它们计算在内:

    self.ref.child('posts').on('value', function (snap) {
      self.posts = snap.val();
      self.keys = Object.keys(self.posts);
      self.count = snap.numChildren();
    });

09-25 22:25