javascript - 模板化中的自定义计算函数 `<template>`

标签 javascript polymer polymer-1.0

使用 polymer Templatizer行为,假设我们一切正常,我们不仅想要 stamp属性,还有计算函数,应该如何处理呢?

我可以通过指定使其工作

_getRootDataHost: function(){
  return this;
}

然而事实证明这样做意味着

<my-element prop="[[value]]"></my-element>

实际上会中断(因为现在在 value 而不是它的父级中查找 <my-element>)。

需要明确的是,上面允许的是

<my-element>
 <template>
  <span>{{someComputedProperty(item)}}</span>
 </template>
</my-element>

哪里someComputedProperty定义于 <my-element>而不是它的容器。

最佳答案

因此,实现此目的的一种极其丑陋的方法是直接在 dataHost 上设置函数,因此我们得到的是在 attached 中调用的函数,它看起来像这样

_registerEvilFunctions: function(){
    this.dataHost._pdt_getArrayItemLabel = this._getArrayItemLabel;
    this.dataHost._pdt_getValidRegexForInput = this._getValidRegexForInput;
}

不能说我对此感到满意,并且我已确保为其添加足够的前缀以防止冲突,但至少它有效。

关于javascript - 模板化中的自定义计算函数 `<template>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33765957/

相关文章:

javascript - 将 `<iron-list>` 与 `<paper-scroll-header-panel>` 组合

javascript - Jquery 选择器无法在 Polymer 中找到 div 元素。应该是直截了当不理解问题

javascript - 在 js 标签中使用 dom-module 属性

javascript - polymer 1.x : Using iron-input to format numbers on input

javascript - $q.all 为所有值返回 undefined

javascript - JQuery - 使用 .innerHTML 附加而不是替换内容?

javascript - 禁止在选择时打字

javascript - 从一个 polymer 组件调用一个函数到另一个

javascript - document.write 重置 body 一次

regex - paper-input-container 中的 pattern 属性是什么?