Polymer 1.0条件dom-repeat问题

时间:2015-06-10 06:41:47

标签: dom polymer polymer-1.0

我有一个这样的自定义元素:



<dom-module id="customer-location">
    <template>
      <template is="dom-repeat" items="{{customers}}" filter="isCustomerFound" observe="cuname item.cuname">
        <template is="dom-repeat" items="{{item.branches}}">
          ...
          ...
        </template>
      </template>
    </template>
</dom-module>
&#13;
&#13;
&#13;

此元素的属性cname的值由index.html

设置

&#13;
&#13;
<customer-location cname="{{params.name}}"></customer-location>
&#13;
&#13;
&#13;

上述值可以很好地转移到customer-location元素中。我可以像<p id="maxp">{{cname}}</p>一样打印它。

现在,在<script>...</script>内我试图使用函数访问属性cname

&#13;
&#13;
<script>
  (function () {
      Polymer({
          // define element prototype here
          is: 'customer-location',
          properties: {
              latlngcombo: { type: String },
              cname: { type: String }
          },
              
          isCustomerFound: function (item) {
              var dm = this.$$("#maxp").innerHTML;  
              return item.cuname == dm;
          },

          ready: function () {
              this.customers = [
                  { "cuname": "msi", "name": "Microsoft India", "addr": "2, Strand Road", "addr2": "Kolkata, IN", "phone": "332.245.9910", "lat": "22.589091", "lng": "88.352359", "branches": [
                      { "branch": "Hyderabad", "email": "hydho@cyfoxpapers.com", "phone": "1582012244", "address": "69/A, Twisted Road, Banjara Hills, Hyderabad: 600001", "code": "CPHYD" }
                      ]
                  },
                  { "cuname": "googindia", "name": "Google India Inc.", "addr": "6/B, Pragati Apartment", "addr2": "Pitampura, New Delhi, IN", "phone": "493.050.2010", "lat": "28.61598", "lng": "77.244382" },
                  { "cuname": "gabonint", "name": "Gabon Internationl", "addr": "187 Kabi Kirandhan Road", "addr2": "Bhadrakali, IN", "phone": "983.193.3166", "lat": "22.665979", "lng": "88.348134" },
                  { "cuname": "itg", "name": "India Tour Guides", "addr": "115/5 Palash Sarani", "addr2": "Amritsar, India", "phone": "943.390.9166", "lat": "31.604877", "lng": "74.572276" },
                  { "cuname": "creatad", "name": "Creative Ad Agency", "addr": "25 BPMB Saranai", "addr2": "McLeodganj, Dharamsala. IN", "phone": "943.390.9166", "lat": "32.232596", "lng": "76.324327" }
                ];
              }
          });
      })();
</script>
&#13;
&#13;
&#13;

函数isCustomerFound旨在检查当前cname值是否与外item.cuname循环中的dom-repeat匹配。

但我永远无法从var mm = this.$$("#maxp").innerHTML;

获得价值

请告诉我我做错了什么。在另一个类似的元素this.$$("#maxp").innerHTML中获得的值很好!

提前致谢

1 个答案:

答案 0 :(得分:1)

这是cname属性绑定在元素范围之外的结果。发生的事情是首先创建<customer-location>元素并将其标记到DOM上,通过readyattached等等。然后它会收到cname的新值,因为它通过其父级绑定传入。

dom-repeat因此首先使用isCustomerFound的{​​{1}}值执行undefined过滤器,但不知道在cname之后重新解析数据已更新。很遗憾,因为cname不是cname集合的属性,因此无法将其放入items,因此您必须在外部观察该属性并手动重新触发observe过滤。

dom-repeat