简体   繁体   中英

Polymer dom-repeat sub property changes child to host wiring

I have a host element binding array of object to child element which has a paper-input to edit its properties. I don't see the value change on input reflected in the host div element. Even though on debug I can see that the host object has the latest edited name . What should I do to get this automatically wired ?

    <!-- Host element -->
<dom-module id="host-item">
    <template>
          <div>
             <div>[[selectedEmployee.name]]</div>
                <template is="dom-repeat" items="[[employees]]" as="employee">
                    <item-edit item="[[employee]]"></item-edit>
                </template>
          </div>
    </template>
    <script>
    Polymer({
                is: 'host-item',
                properties: {                
                    selectedEmployee: {
                        type: Object 
                    },
                    employees: {
                      type: Array,
                      value = [ { name: 'Name 1'}, { name: 'Name 2'}, { name: 'Name 2'}]
                    }
                },
                ready: function() {
                  this.selectedEmployee = this.employees[0];
                }
            }); 
    </script>
</dom-module>

<!-- Child element -->
<dom-module id="item-edit">
    <template>
          <paper-input id="input" value="{{item.name}}" error-message="Invalid name!"></paper-input>            
    </template>
    <script>
    Polymer({
                is: 'item-edit',
                properties: {                
                    item: {
                        type: Object
                    }
                }
            }); 
    </script>
</dom-module>

Use {{employee}} for 2 way binding . [[...]] is for one way only. Use notify: true on property definition.

Child element should be defined before the parent.

Here is the working example Plunk , and similar Plunk

<item-edit item="{{employee}}"></item-edit>
...
employee: {
                type: Object,
                notify: true,
                value: function () { return {name: 'Test' }; }
            }

Update:

Now "employees" data is in form of an array of objects. Check out this question for working with arrays: Polymer, issue with binding array to paper slider value

Plunk

Docs: Binding to array items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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