<dom-if> in <dom-repeat> 不会在项目更改时调用方法

<dom-if> in <dom-repeat> doesn't call method on item change

我正在尝试根据函数中调用的条件来显示或不显示文本。

我有以下片段:

<dom-module id="lfr-defis">
<template>
    <style is="custom-style" include="iron-flex iron-flex-alignment"></style>
    <style is="custom-style">
        h4{
            margin-top: 10px;
            margin-bottom: 0px;
        }

        paper-card{
            margin-top: 5px;
        }

        p{
            margin-top: 5px;
            margin-bottom: 0px;
        }
    </style>
    <div>
        <paper-icon-button icon="add" on-tap="newDefi" style="float: right"></paper-icon-button>
    </div>
    <div style="margin-top: 5px">
        <template is="dom-repeat" items="[[defis]]">
            <paper-card style="width: 100%">
                <div class="card-content">
                    <div class="layout horizontal">
                        <paper-input label="Jeu" class="flex" always-float-label value="{{item.gameName::change}}"></paper-input>
                        <div style="margin:5px"></div>
                        <paper-input label="Incentive" class="flex" always-float-label value="{{item.incentiveName::change}}"></paper-input>
                    </div>
                    <label for="datetime"><b>Date : </b></label><datetime-input id="datetime" value="{{item.date::change}}"></datetime-input>
                    <h4>Défi FR</h4>
                    <div class="layout horizontal justified">
                        <paper-input value="{{item.fr.name::change}}" class="flex">
                            <iron-icon icon="label" slot="prefix"></iron-icon>
                        </paper-input>
                        <div style="margin:5px"></div>
                        <paper-input value="{{item.fr.amount::change}}" type="number">
                            <div slot="prefix">$ </div>
                        </paper-input>
                        <div style="margin:5px"></div>
                        <paper-input value="{{item.fr.rank::change}}" type="number">
                            <iron-icon icon="editor:format-list-numbered" slot="prefix"></iron-icon>
                        </paper-input>
                    </div>

                    <h4>Concurrent direct</h4>
                    <div class="layout horizontal justified">
                        <paper-input value="{{item.concurrent.name::change}}" class="flex">
                            <iron-icon icon="label" slot="prefix"></iron-icon>
                        </paper-input>
                        <div style="margin:5px"></div>
                        <paper-input value="{{item.concurrent.amount::change}}" type="number">
                            <div slot="prefix">$ </div>
                        </paper-input>
                        <div style="margin:5px"></div>
                        <paper-input value="{{item.concurrent.rank::change}}" type="number">
                            <iron-icon icon="editor:format-list-numbered" slot="prefix"></iron-icon>
                        </paper-input>
                    </div>

                    <template is="dom-if" if="{{!hasAllFields(item)}}">
                        <p style="color:red">Tous les champs doivent être renseignés</p>
                    </template>
                    <template is="dom-if" if="{{!validRank(item.fr.rank, item.concurrent.rank)}}">
                        <p style="color:red">Un des rangs doit être égal à 1</p>
                    </template>
                    <template is="dom-if" if="{{!validAmount(item.fr.rank, item.concurrent.rank, item.fr.amount, item.concurrent.amount)}}">
                        <p style="color:red">Le montant du 1er doit être supérieur à l'autre</p>
                    </template>
                </div>
                <div class="card-actions">
                    <paper-icon-button title="Sauvegarder" icon="save" on-tap="save" data-item$="{{item}}" style="color:deepskyblue"></paper-icon-button>
                    <paper-icon-button title="Annuler" icon="cancel" on-tap="cancel" data-item$="{{item}}" style="color:lightcoral"></paper-icon-button>
                    <paper-icon-button title="Supprimer" icon="delete" on-tap="delete" data-item$="{{item}}" style="float:right"></paper-icon-button>
                    <paper-icon-button title="Archiver" icon="archive" on-tap="archive" data-item$="{{item}}" style="float:right; color:gray"></paper-icon-button>
                </div>
            </paper-card>
        </template>
    </div>

</template>


<script src="defis.js"></script>
</dom-module>

Javascript :

class Defis extends Polymer.Element {
    static get is() {
        return 'lfr-defis';
    }

    static get properties() {
        return {
            defis: {
                type: Array,
                value: []
            }
        }
    }

    constructor() {
        super();
    }

    newDefi() {
        this.push('defis', {
            id: + new Date(),
            date: + new Date(),
            fr: {},
            concurrent: {}
        });
    }

    save(event) {
        const item = this.getItem(event);
        const index = this.defis.findIndex(d => d.id === item.id);
        if(index !== -1){
            this.defis[index] = item;
        }
        else{
            this.push('defis', item);
        }
    }

    cancel(event) {
        const item = this.getItem(event);
        const index = this.defis.findIndex(d => d.id === item.id);
        if(index !== -1){
            this.set('defis.' + index, item);
        }
    }

    delete(event) {
        const item = this.getItem(event);
        const index = this.defis.findIndex(d => d.id === id);
        this.splice('defis', index, 1);
    }

    archive(event) {
        const item = this.getItem(event);
        const index = this.defis.findIndex(d => d.id === id);
        this.splice('defis', index, 1);
    }

    getItem(event) {
        return JSON.parse(event.target.dataset.item);
    }

    hasAllFields(item, trigger) {
        return item.gameName && item.incentiveName && item.date &&
            item.fr.name && item.fr.amount && item.fr.rank &&
            item.concurrent.name && item.concurrent.amount && item.concurrent.rank;
    }

    validRank(rankA, rankB) {
        return rankA && rankA === '1' || rankB && rankB === '1';
    }

    validAmount(rankA, rankB, amountA, amountB) {
        return rankA > rankB && parseInt(amountA) < parseInt(amountB) ||
            rankA < rankB && parseInt(amountA) > parseInt(amountB) ||
            rankA === rankB && parseInt(amountA) === parseInt(amountB);
    }

    isValid(item) {
        return this.hasAllFields(item) && this.validRank(item.fr.rank, item.concurrent.rank) &&
            this.validAmount(item.fr.rank, item.concurrent.rank, item.fr.amount, item.concurrent.amount);
    }
}

customElements.define(Defis.is, Defis);

我遇到的问题是 hasAllFields(item) 函数仅在页面加载时调用一次,而不是在我更改 <paper-input> 字段中的值时调用(这是预期的行为)。因此,由于函数 returns false 在加载时显示了消息,但它从未更新,因此当我的所有字段都已填充时不会隐藏。

我发现这可能与 <dom-repeat>item 元素的使用有关,但我找不到任何解决方案。

尝试改变你的方法以通过也item.gameName

hasAllFields(item,item.gameName)

也更改您的函数以增加一个参数,即使您不会使用它

实际上 Polymer 不会检测到对象项中的更改,但它应该在子属性中检测到

还有一点,你可能需要 value="{{item.gameName::input}}"value="{{item.gameName::change}}"

如果你想让 Polymer 更新 DOM 中的子属性,那么你需要将这些子属性添加到方法调用中。 Polymer 不够智能,无法理解何时更新子属性。

<template is="dom-if" if="{{!hasAllFields(item.gameName, item.incentiveName, tem.date, item.fr.name, item.fr.amount, item.fr.rank, ), item.concurrent.name, item.concurrent.amount, item.concurrent.rank}}">

另一种方法是添加一个观察者,它检查与上面相同的属性,每次属性更改时更新一个 int 属性,然后将该 int 传递给 hasAllFields,因为例如 hasAllFields(inputUpdatedInt)。第三种方法是在输入上添加一个侦听器,然后在设置新值之前使用 this.set('debris', [] 对碎片进行硬重置,因为这实际上会更改您现在正在检查的 item