聚合物 dom 重复 - 使用关联阵列

Polymer Dom Repeat - Using Associative Arrays

本文关键字:关联 阵列 dom 重复 聚合物      更新时间:2023-09-26

是否可以将 Polymer dom-repeat 与关联数组一起使用,如以下代码所示?

<template is="dom-repeat" items="{{array}}">{{item}}</template>
// script
Polymer({
  ...
  properties: {
    array: {
       type: Array
       value: []
    }
  },
  ready: function(){
    this.set('array.someUID', 'someValue');
    this.set('array.someUID', 'someValue');
    this.set('array.someUID', 'someValue');
  }
});

我知道这个例子行不通,在处理这个问题时,我不得不将其转换为普通数组。但是我希望数组中项目的 ID 与数据库中的 ID 相同,这样当值更新时,我只需调用一个函数来更新项目。

this.set('array.' + uid, newValue);

我了解,associative arrayjavascript中被计算为Objectdom-repeat不适用于Object.如果您将数据重新设计为类似

[{
  key:{{uid}},
  value:someValue
 }]

然后,您可以在其上使用dom-repeat