简体   繁体   English

聚合物动态创建的元素不起作用(iron-scroll-threshold)

[英]Polymer dynamically created element not working (iron-scroll-threshold)

I want to create notifications that feel like Facebook's. 我想创建类似Facebook的通知。 I use polymer and the elements: iron-dropdown , iron-list , iron-scroll-threshold . 我使用聚合物和以下元素: iron-dropdowniron-listiron-scroll-threshold I want to create an iron-scroll-threshold element after the dropdown opens. 我想在下拉列表打开后创建一个iron-scroll-threshold元素。 When I create it first it will fire an event and start loading so I use it like this: 当我第一次创建它时,它将触发一个事件并开始加载,因此我可以这样使用它:

 <template>
        <iron-ajax
                id="ajax"
                url="some url"
                handle-as="json"
                on-response="_handleResponse">
        </iron-ajax>

        <paper-button class="status-bar-button" on-tap="open">
            <iron-icon icon="social:notifications-none"></iron-icon>
        </paper-button>

        <iron-dropdown id="dropdown"
                       vertical-align="[[verticalAlign]]"
                       horizontal-align="[[horizontalAlign]]"
                       disabled="[[disabled]]"
                       open-animation-config="[[openAnimationConfig]]"
                       close-animation-config="[[closeAnimationConfig]]"
                       horizontal-offset="[[horizontalOffset]]"
                       vertical-offset="[[verticalOffset]]">

            <div id="sidebar-apps-container" class="dropdown-content shadow-elevation-8dp">
                <div class="beeper" style="right:128px"></div>
                <div class="apps-body" id="scrollThresholdtarget">
                    <iron-list id="zoznam" items="[]" as="notifs" scroll-target="scrollThresholdtarget">
                        <template>
                            <a href="[[notifs.link]]" style="height: 150px">[[notifs.content]]</a><br/>
                        </template>
                    </iron-list>
                </div>
            </div>
        </iron-dropdown>

        <div id="forThreshold"></div>
    </template>

And the script goes like this: 脚本如下所示:

open: function () {
    if (!this.scrollInitialised) {
        this.initScrollThreshold();
        this.scrollInitialised = true;
    }

    this.$.dropdown.open();
},

/**
 * @method
 */
initScrollThreshold: function () {
    var scrollThreshold = document.createElement('iron-scroll-threshold');
    scrollThreshold.setAttribute('id', 'threshold');
    scrollThreshold.setAttribute('on-lower-threshold', '_loadData');
    scrollThreshold.setAttribute('lower-threshold', '200');
    scrollThreshold.setAttribute('scroll-target', 'scrollThresholdtarget');
    this.$.forThreshold.appendChild(scrollThreshold);
    this._loadData();
},

/**
 * @method
 */
_handleResponse: function (event) {
    var data = event.detail.response;

    console.log(data);
    var elem = this.$.zoznam;

    data.forEach(function(notifs) {
        elem.push('items', notifs);
    });

    document.querySelector('#threshold').clearTriggers();
    console.log('fired');
},

/**
 * @method
 */
_loadData: function() {
    console.log('fired request');
    this.$.ajax.generateRequest();
}

But when elements are created it doesn't work. 但是,当创建元素时,它不起作用。 The event will never be fired. 该事件将永远不会被触发。

Have you tried using addEventListener for the event you are trying to bind to via attributes? 您是否尝试过使用addEventListener来尝试通过属性绑定到的事件? My guess is that isn't getting handled right (since Polymer doesnt support programmaticly binding things yet). 我的猜测是处理不正确(因为Polymer尚不支持以编程方式绑定事物)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM