简体   繁体   English

Polymer 0.9“过滤器”的最佳方法是什么?

[英]What is the best way of Polymer 0.9 “filters”?

In 0.5 it was possible to add a JS-functions like: 在0.5中可以添加JS函数,例如:

PolymerExpressions.prototype.timeofdate = function(input) {
 if(input) {
   return input.substring(11,16)
}

(Extracting hour:minute from "MongoDB-timestamp" like 2014-10-04T12:34:56+02:00) (提取时间:距离“ MongoDB时间戳记”分钟,如2014-10-04T12:34:56 + 02:00)

And use it with piped variable like: 并与管道变量一起使用,例如:

{{starts | timeofdate}}

When I tried to upgrade the code above to 0.9, I had to create this element instead: 当我尝试将上述代码升级到0.9时,我不得不创建以下元素:

<script>
  Polymer({
    is: 'x-substr',
    properties: {
      start: Number,
      end: Number,
    },
    attached: function() {
      this.innerHTML = this.innerHTML.substring(this.start, this.end);
    }
  });
</script>

And use it like this: 并像这样使用它:

<x-substr start="11" end="16">{{starts}}</x-substr>

(Use "attached" callback instead of "ready", if you should use this element with any data binding) (如果您应该将此元素与任何数据绑定一起使用,请使用“附加的”回调,而不要使用“就绪”)

Is this the "right way" to do filter functionality like above in Polymer 0.9+? 这是否是“过滤器”功能的“正确方法”,如上面的Polymer 0.9+?

The closest you will get to the filter behaviour in 0.5 are computed bindings in 0.9+. 0.5中最接近过滤器行为的是0.9+中的计算绑定

For your example this would be something like this: 以您的示例为例:

<dom-module id="...">
  <template>
    ...
    <span>{{timeofdate(starts)}}</span>
    ...
   </template>
<dom-module>

Polymer({
  ...
  timeofdate: function (input) {
    return input.substring(11,16);
  }
  ...
});

If you need this time in more than one place you could also make it a computed property instead. 如果您需要在多个地方使用此时间,也可以将其设为计算属性

<dom-module id="...">
  <template>
    ...
    <span>{{starttime}}</span>
    ...
   </template>
<dom-module>

Polymer({
  ...
  properties: {
    starts: String,
    starttime: {
      type: String,
      computed: 'timeofdate(starts)'
    }
  },
  timeofdate: function (input) {
    return input.substring(11,16);
  }
  ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM