Polymer事件侦听器提取数据

时间:2014-08-13 11:51:27

标签: polymer addeventlistener

如何使用此订阅数据设置父元素列表。? 不希望为subscription元素更改或添加属性,因为它独立于父元素。

我得到了这样的聚合物元素结构:

<polymer-element name="parent">
 <polymer-element name="subscription" id="subscription">
 <polymer-element>
  <div id="list">
    <template repeat="{{item in list}}">
      <item name="{{item}}">
      </item>
    </template>
  </div>
 <script>
  Polymer('parent', {
    ready: function() {
     this.list = this.list || [];
     subscribe = this.$.subscription;

//Anytime if there is any change in subscription it send the data. 

     subscribe.addEventListener('data_from_subscription_element', function(x) {

      // How to set list which is of parent element with this subscription data.?
      // Don't want to alter or add attribute to subscription element as it's independent of parent.  

       this.data;
     });
    }
  });
 </script>
<polymer-element>

1 个答案:

答案 0 :(得分:1)

这似乎是改变你所在范围的常见问题。试试这个:

<script>
  Polymer('parent', {
    ready: function() {
      var that = this; // Giving the current scope to that;

      this.list = this.list || [];
      subscribe = this.$.subscription;

      subscribe.addEventListener('data_from_subscription_element', function(x) {
        that.list = this.data; // Using that here 
      });
    }
  });
</script>