【问题标题】:Polymer 1.0 dom-repeat is not notified properly when array changes数组更改时,Polymer 1.0 dom-repeat 未正确通知
【发布时间】:2016-01-03 06:22:31
【问题描述】:

我有一个<paper-dialog> 模式,它的作用类似于邀请新候选人接受挑战的表格。在<invite-candidates> 组件中有一个dom-repeat,它为candidates 列表中的每个对象呈现一个复杂的输入(<invite-candidate-input>)。

每次我打开(切换)该模式时,我都会清除列表并添加一个要呈现的新对象。第一次打开页面并打开模式时,我会看到一个输入,正如预期的那样。但是如果我关闭模式然后再次打开它,它会被清除,一个新对象被添加到列表中,但dom-repeat 没有得到通知。当我单击按钮添加另一个输入时,它被添加到列表中并呈现。但是我的列表现在有 2 个对象,并且只有 1 个 <invite-candidate-input>

<dom-module id="candidate-page">
<template>
    <button id="createActionButton" on-click="_toggleDialog"></button>
    <invite-candidates id="inviteCandidates"></invite-candidates>
</template>
<script>
    Polymer({
        is: 'candidate-page',
        properties: {
            Address: String,
            challenges: {
                type: Array,
                notify: true
            }
        },
        _toggleDialog: function () {
            this.$.inviteCandidates.toggle();
        }
    });
</script>

<dom-module id="invite-candidates">
<template>
    <paper-dialog id="dialog" modal style="background: white;">
        <form is="iron-form" id="form">
            <template is="dom-repeat" items="{{candidates}}">
                <invite-candidate-input candidate="{{item}}"></invite-candidate-input>
            </template>
            <div id="addCandidate" on-click="_addCandidate">
                <iron-icon icon="add"></iron-icon>
                <span>Add another candidate</span>
            </div>
            <div class="buttonGroup">
                <paper-button class="primary" raised on-click="inviteCandidates" dialog-confirm>Invite
                </paper-button>
            </div>
        </form>
    </paper-dialog>
</template>
<script>
    Polymer({
        is: 'invite-candidates',
        properties: {
            candidates: {
                type: Array,
                notify: true,
                value: []
            }
        },
        listeners: {
            'candidate-removed': '_removeCandidate'
        },
        toggle: function () {
            this._clearDialog();
            this._addCandidate();
            this.$.dialog.toggle();
        },
        _addCandidate: function () {
            this.push('candidates', {});
        },
        _removeCandidate: function(event){
            var index = this.candidates.indexOf(event.detail);
            this.splice('candidates', index, 1);
        },
        _clearDialog: function () {
            var el;
            while ((el = document.querySelector('invite-candidate-input')) !== null) {
                el.remove();
            }
            this.candidates = [];
        }
    });
</script>

<dom-module id="invite-candidate-input">
<template>
    <div id="inputContainer">
        <paper-input id="address" label="Address" value="{{candidate.Address}}"></paper-input>
        <iron-icon icon="remove" on-click="_removeItem"></iron-icon>
    </div>
    <br>
</template>
<script>
    Polymer({
        is: 'invite-candidate-input',
        properties: {
            challenges: {
                type: Array
            },
            candidate: {
                type: Object,
                reflectToAttribute: true,
                notify: true
            }
        }
        _removeItem: function () {
            this.fire('candidate-removed', this.candidate);
            this.remove();
        }
    });
</script>

【问题讨论】:

  • 我注意到您在致电 _clearDialog 时未正确通知。尝试转this.candidates = []; ==> this.set('candidates', []) 重置阵列。

标签: javascript dom data-binding polymer polymer-1.0


【解决方案1】:

这是因为在_clearDialog 中,你使用了this.candiates = [];。如果你这样做 this.splice('candidates', this.candidates.length) 它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多