javascript - 单击 Polymer 中的按钮加载更多内容

标签 javascript html dom polymer lazy-loading

在 Polymer 中,我尝试延迟加载 DOM 中的内容。有一个使用 iron-scroll-threshold 在滚动时执行此操作的示例。但是如何在按钮单击上实现相同的效果?

<head>
  <base href="https://polygit.org/polymer+1.5.0/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="iron-list/iron-list.html">
  <link rel="import" href="iron-scroll-threshold/iron-scroll-threshold.html">
  <link rel="import" href="paper-progress/paper-progress.html">
</head>
<body>
<x-foo></x-foo>

<dom-module id="x-foo">
  <template>
    <style>
      iron-list {
        height: 400px;
      }
    </style>

    <iron-scroll-threshold id="threshold"
                           lower-threshold="50"
                           on-lower-threshold="_loadMoreData"
                           lower-triggered="{{nearBottom}}">
      
      <iron-list scroll-target="threshold" items="[[items]]">
        <template>
          <div>[[index]]: [[item]]</div>
        </template>
      </iron-list>
    </iron-scroll-threshold>
<button on-tap="handleTap">Load More</button>
    <template is="dom-if" if="[[nearBottom]]">
      <paper-progress indeterminate></paper-progress>
    </template>
  </template>

  <script>
    HTMLImports.whenReady(function() {
      Polymer({
        is: 'x-foo',
        properties: {
          items: {
            type: Array,
            value: function() { return []; }
          }
        },
        handleTap: function() {
        _loadMoreData();
      },
        _loadMoreData: function() {
          console.log('loading 100 more...');

          // simulate network delay
          this.async(function() {
            for (let i = 0; i < 50; i++) {
              this.push('items', Math.random());
            }
            this.$.threshold.clearTriggers();
          }, 500);
        }
      });
    });
  </script>
</dom-module>
</body>

Here's the code that I'm trying out

最佳答案

您缺少这个:

handleTap: function() {
    this._loadMoreData();
}

关于javascript - 单击 Polymer 中的按钮加载更多内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39207898/

相关文章:

javascript - 如何将元素从字符串插入到html文档中?

javascript - 一键清除 jquery 中的搜索栏

javascript - 查看网页和桌面应用程序是否在同一台计算机上启动

javascript - 如何以编程方式将图片上传到 Facebook?

javascript - 简单的文本字段计算不起作用

javascript - 尝试使用 JQuery 构建具有可扩展行(嵌入式表)的表

javascript - 如何使用 MooTools 以编程方式单击元素?

javascript - ReactJS:去抖一个以状态值作为参数的函数;最好的方法是什么?

javascript - 如何使用javascript在html上获取上个月和下个月的结果

javascript - DOM parentNode 和 parentElement 的区别