在聚合物中第一个列表滚动结束后,永远不会执行 iron-scroll-threshold

iron-scroll-threshold is never executed after first list scolling is end in polymer

我通过使用 iron-listiron-scroll-threshold 完成了以下操作,以便在聚合物中实现无限滚动。但问题是 iron-scroll-threshold 的 loadMoreData 在聚合物中的第一个列表滚动结束后永远不会执行。请帮助我在我的代码中缺少或错误的那个。谢谢

<template>

    <iron-ajax id="ajax" 
      url="https://randomuser.me/api/" 
      handle-as="json" 
      params='{"results": 20}'
      loading="{{loadingPeople}}"
      on-response="categoriesLoaded">
    </iron-ajax>

    <app-toolbar>
      <div main-title>Load data using iron-scroll-threshold</div>
    </app-toolbar>

    <iron-scroll-threshold on-lower-threshold="loadMoreData" id="threshold">
      <iron-list id="list" items="[[categoriesJobs]]" as="person" scroll-target="threshold">
        <template>
          <div>
            <div class="personItem" tabindex$="[[tabIndex]]">
              <iron-image class="avatar" sizing="contain" src="[[person.picture.medium]]"></iron-image>
              <div class="pad">
                <div class="primary">[[person.name.first]] [[person.name.last]]</div>
                <address>
                  [[person.location.street]] [[person.city]] <br />
                  [[person.location.city]], [[person.location.state]] [[person.location.postcode]]
                </address>
              </div>
            </div>
          </div>
        </template>
      </iron-list>
    </iron-scroll-threshold>

  </template>

  <script>
    Polymer({
      is: 'job-category',
      attached: function () {
        this.companyDetail = [];
        this.categoriesJobs = [];
      },
      loadMoreData: function () {
        this.$.ajax.generateRequest();
      },
      categoriesLoaded: function (e) {
        var people = e.detail.response.results;
        this.categoriesJobs = people;
        this.$.threshold.clearTriggers();
      }
    });
  </script>

</dom-module>

问题出在 iron-list,来自 docs:

iron-list must either be explicitly sized, or delegate scrolling to an explicitly sized parent ...

您可以在这里找到不同的方法,但您可以在下面看到一个工作示例。在此示例中,我还修复了 categoriesLoaded 函数,因此它将加载更多数据,而不仅仅是替换旧数据。

JSBin example

我也把代码放在这里:

<dom-module id="test-app">
  <template>

    <style>

      :host {

      }

      app-toolbar {
        height: 64px;
        background-color: grey;
      }

      iron-list {
        flex: 1 1 auto;
      }

      .container {
        height: calc(100vh - 64px);
        display: flex;
        flex-direction: column;
      }

    </style>

    <iron-ajax id="ajax" 
      url="https://randomuser.me/api/" 
      handle-as="json" 
      params='{"results": 20}'
      loading="{{loadingPeople}}"
      on-response="categoriesLoaded">
    </iron-ajax>

    <app-toolbar>
      <div main-title>Load data using iron-scroll-threshold</div>
    </app-toolbar>

    <div class="container">
      <iron-scroll-threshold on-lower-threshold="loadMoreData" id="threshold">
        <iron-list id="list" items="[[categoriesJobs]]" as="person" scroll-target="threshold">
          <template>
            <div>
              <div class="personItem" tabindex$="[[tabIndex]]">
                <iron-image class="avatar" sizing="contain" src="[[person.picture.medium]]"></iron-image>
                <div class="pad">
                  <div class="primary">[[person.name.first]] [[person.name.last]]</div>
                  <address>
                    [[person.location.street]] [[person.city]] <br />
                    [[person.location.city]], [[person.location.state]] [[person.location.postcode]]
                  </address>
                </div>
              </div>
            </div>
          </template>
        </iron-list>
      </iron-scroll-threshold>
    </div>

  </template>
  <script>
    Polymer({

      is: 'test-app',

      attached: function() {
        this.companyDetail = [];
        this.categoriesJobs = [];
      },

      loadMoreData: function () {
        console.log('load more data');
        this.$.ajax.generateRequest();
      },

      categoriesLoaded: function (e) {
        var self = this;
        var people = e.detail.response.results;
        people.forEach(function(element) {
          self.push('categoriesJobs', element);
        });
        this.$.threshold.clearTriggers();
      }

    });
  </script>
</dom-module>