Polymer / polymer

Our original Web Component library.
https://polymer-library.polymer-project.org/
BSD 3-Clause "New" or "Revised" License
22.03k stars 2.02k forks source link

setProperties not work #5521

Closed zarudYin closed 5 years ago

zarudYin commented 5 years ago

Description

When using setProperties to change 2 sub-attributes, the sub-element cannot display 2 changes correctly. Only the first sub-property change in the object will be displayed.

Code

class PolymerDemoApp extends PolymerElement {
    static get template() {
        return html`
            <button on-click="handleClick">Click</button>
            <info-area info-obj="{{user}}"></info-area>
        `;
    }

    static get properties() {
        return {
            user: {
                type: Object,
                value: {
                    name: 'tom',
                    sex: 'man'
                }
            }
        };
    }

    handleClick() {
        this.setProperties({ 'user.name': 'jack', 'user.sex': 'woman' });
    }
}

window.customElements.define('polymer-demo-app', PolymerDemoApp);

class InfoArea extends PolymerElement {
    static get template() {
        return html`
            <div>name: {{infoObj.name}}, sex: {{infoObj.sex}}</div>
        `;
    }

    static get properties() {
        return {
            infoObj: {
                type: Object,
                value: null
            }
        };
    }
}

window.customElements.define('info-area', InfoArea);

image

after click button

image

the sex is not change

Versions

kevinpschaaf commented 5 years ago

Batching of path changes via setProperties is not supported for this reason. See the API docs here: https://polymer-library.polymer-project.org/3.0/api/polymer-element#PolymerElement-method-setProperties