聚合物滤波器表达式的上下文

时间:2014-10-20 19:21:31

标签: javascript polymer web-component

在过滤器表达式中引用实际聚合物元素的最佳方法是什么?似乎this引用了父template对象。如果我需要在过滤器中访问实际聚合物元素的属性,我甚至可以这样做吗?元素最初是否未完全创建,使得在模板化过程中访问元素无关紧要?

编辑:过滤器位于重复模板

简化示例:

<template repeat="{{item in items}}">
  <div>{{prop | filterMethod}}</div>
</template>
<script>
...
filterMethod : function(v){
  return this.someProp + v;
},
...
</script>

1 个答案:

答案 0 :(得分:1)

我可能误解了您的问题,但是当您在元素中定义表达式时,您可以访问this

<polymer-element name="my-element">
  <template>
    <template repeat="{{thing in things}}">
      <div>{{thing | reverse}}</div>
    </template>
  </template>
  <script>
    Polymer({
      val: "foo",
      created: function() {
        this.things = ['ABC', 'DEF', 'XYZ'];
      },
      reverse: function(value) {
        return (this.val + " " + value).split('').reverse().join('');
      }
    });
  </script>
</polymer-element>

这是输出:

CBA oof
FED oof
ZYX oof

您可以尝试http://jsbin.com/yuvoqu/edit

上的示例