多路径对象变异数组

时间:2018-05-06 13:41:53

标签: polymer-1.0

想要改变一个Obj阵列的路径。

对象应该在点击时更改,它看起来像这样:

<iron-icon id="id" icon="icons:arrow-downward" on-click="_sortTags"
class$="arrow [[sortData.id.icon]] [[sortData.id.state]]"></iron-icon>

这里我想改变sortData Obj,点击上面的图标

会触发此功能
_changeSortData(field,order,iconShape,status){ //there is a function calls this function but did not bring it here to make issue simple
        this.set('sortData[field].sort', order);
        this.set('sortData[field].icon', iconShape);
        this.set('sortData[field].state', status);

      }

下面的对象是属性:

sortData: {
          type: Object,
          value: function () {
            return {
              "id": {
                "icon": "downward",
                "sort": "default",
                "state": "inactive"
              },
              "date": {
                "icon": "downward",
                "sort": "default",
                "state": "inactive"
              }
            }
          },
        },

现在可以在此处转义单个qoutes以将[field]应用为pram

this.set('sortData[field].sort', order);

因为sortData Obj(id和data)中存在两个字段

1 个答案:

答案 0 :(得分:1)

this.set(path, value)中,path可以指定为string Array 。由于您有动态路径部分,因此您将使用Array路径,如下所示:

this.set(['sortData', field, 'sort'], order); // `field` is dynamic

demo