0

Here is a snippet:

<template is="dom-repeat" items="{{zones}}" as="zone">
  <paper-button on-tap="_openSettings">Open Settings
  </paper-button>
  <paper-dialog id="[[_getZoneSettingsId(zone.id)]]" class="zone-settings">
    <input type="button" class="delete-zone" value="Delete zone" on-tap="_deleteZone"/>
  </paper-dialog>
</template>

<script>   
  Polymer({
    ...,
    _openSettings: function(e) {
      var zone = e.model.zone;
      this.$$("#" + this._getZoneSettingsId(zone.id)).open();
    },

    _getZoneSettingsId: function(zoneId) {
      return "zone-settings-" + zoneId.toString();
    },

    _deleteZone: function(e) {
      var zone = e.model.zone;
      var zones = this.zones.slice(0);
      for (var i = zones.length - 1; i >= 0; i--) {
        if(zone.id === zones[i].id) {
          zones.splice(i, 1);
        }
      }
      this.set("zones", zones);
    }
  })
</script>

One of the things that can be done in this dialog is deleting the zone from the zones list. This works fine as long as I delete the last zone in the list. However, if I have two zones and delete the first one, the dialog for the second one automatically opens as though it has replaced the first one.

I assume this has to do with how dom-repeat works, but I am not sure how to fix it. Any clues?

4

1 回答 1

1

要通知聚合物有关数组的变化,您必须使用数组突变函数。所以在你的情况下你可以使用this.splice();

所有这些功能都可以在这里找到:https ://www.polymer-project.org/1.0/docs/devguide/model-data#array-mutation

于 2017-05-18T06:46:19.633 回答