dom-repeat中的聚合物可观察对象未获得更新

时间:2017-09-23 14:52:48

标签: javascript dom polymer

我正在尝试在dom-repeat中显示包含数组的对象。 数据结构如下: answerObject:{firstQuestion:[“one”,“two”],                第二个问题:[“三”,“四”]}

我在嵌套的dom-repeat中使用计算绑定将对象转换为数组,然后使用另一个dom-repeat来显示数组的内容。

  <ul>
    <template is="dom-repeat" items="[[_makeArray(answerObject)]]" as="question">
      <li>[[question.key]]</li>

      <ul>
          <template is="dom-repeat" items="[[question.listOfAnswer]]" as="answer">
              <li>[[answer]]</li>
          </template>
      </ul>
 </template>

我创建了answerObject属性,如下所示:

  answerObject: {    
     type: Object,
     notify: true,
     value: {firstQuestion: ["one", "two", "three"], 
             secondQuestion: ["four","five"] },
     observer: '_answerChanged'   
  },

我尝试了所有不同的方法来观察对象或数组,并且没有触发函数'_answerChanged'和'_makeArray'。

   /* mutating object and then notifyPath */
   this.set('answerObject.firstQuestion', ["newone"]);
   this.notifyPath('answerObject.firstQuestion');

   /* mutating array then notifySplices */
   this.push('answerObject.secondQuestion',"six");
   this.notifySplices('answerObject.secondQuestion', [
     {index:3, removed: [], addedCount: 1, object: _this.answerObject.secondQuestion, type:'splice'}
   ]);

   /* dirty check */
   var array = this.answerObject.firstQuestion;
   this.answerObject.firstQuestion=[];
   this.answerObject.firstQuestion = array;

有什么建议我错过了什么?谢谢。

1 个答案:

答案 0 :(得分:0)

未触发观察者,因为您的方案不受支持。

根据official documentation

  

不支持原始数组项。这是因为基元(如数字,字符串和布尔值)具有相同的   值由同一个对象表示。

目前推荐的解决方法(在我之前的链接中也有说明)是,您只需使用简单对象包装原始值。

所以,而不是:

["one", "two", "three"]

你使用这样的东西:

[{ value: "one" }, { value: "two" }, { value: "three" }]
相关问题