Javascript 聚合物结合是模板内的一种功能结果
我试图避免在Javascript 聚合物结合是模板内的一种功能结果,javascript,polymer-2.x,Javascript,Polymer 2.x,我试图避免在dom repeat中多次调用同一个函数 例如: <template is="dom-repeat" items="..." as="column"> <template result="_myFunction(column, index)"> <div>Primary: [[result.primary]]</div> <div>Secondary: [[result.secondary]]</d
dom repeat
中多次调用同一个函数
例如:
<template is="dom-repeat" items="..." as="column">
<template result="_myFunction(column, index)">
<div>Primary: [[result.primary]]</div>
<div>Secondary: [[result.secondary]]</div>
...
</template>
</template>
...
_myFunction(column, index) {
return { primary: "1", secondary: "2", ......}
}
主要:[[result.Primary]]
次要:[[result.Secondary]]
...
...
_myFunction(列、索引){
返回{primary:“1”,secondary:“2”,…}
}
但是我找不到正确的方法来做这件事,目前我正在调用我所有的
中的\u myFunction(column,index)
如何避免多次调用该函数?试试下面的代码,它是使用DOM repeat的polymer自定义元素的工作示例,希望这对您有用
<dom-module id="your-template-container">
<template>
<template is="dom-repeat" items="{{data}}" as="column">
<div>Primary: [[column.primary]]</div>
<div>Secondary: [[column.secondary]]</div>
</template>
</template>
<script>
Polymer({
is: 'your-template-container',
ready: function() {
// Sample object data is below
this.data = [
{primary: 1, secondary: 2},
{primary: 1, secondary: 2},
{primary: 1, secondary: 2}
];
}
});
</script>
</dom-module>
<!-- THE FOLLOWING WILL RENDER YOU POLYMER CUSTOM ELEMENT WITH DOM REPEAT-->
<your-template-container></your-template-container>
主:[[column.Primary]]
辅助:[[column.Secondary]]
聚合物({
是:'您的模板容器',
就绪:函数(){
//下面是示例对象数据
此参数。数据=[
{小学:1,中学:2},
{小学:1,中学:2},
{小学:1,中学:2}
];
}
});
试试下面的代码,它是使用DOM repeat的聚合物自定义元素的工作示例,希望这对您有用
<dom-module id="your-template-container">
<template>
<template is="dom-repeat" items="{{data}}" as="column">
<div>Primary: [[column.primary]]</div>
<div>Secondary: [[column.secondary]]</div>
</template>
</template>
<script>
Polymer({
is: 'your-template-container',
ready: function() {
// Sample object data is below
this.data = [
{primary: 1, secondary: 2},
{primary: 1, secondary: 2},
{primary: 1, secondary: 2}
];
}
});
</script>
</dom-module>
<!-- THE FOLLOWING WILL RENDER YOU POLYMER CUSTOM ELEMENT WITH DOM REPEAT-->
<your-template-container></your-template-container>
主:[[column.Primary]]
辅助:[[column.Secondary]]
聚合物({
是:'您的模板容器',
就绪:函数(){
//下面是示例对象数据
此参数。数据=[
{小学:1,中学:2},
{小学:1,中学:2},
{小学:1,中学:2}
];
}
});
我找到了一种避免使用缓存多次调用\u myFunction
的方法:
Before the `dom-repeat`:
execute the function
cache the result in an object
In the `dom-repeat`
use the cached-object to retrieve the value
好处:使用memonization也可能有效我找到了一种方法,可以避免使用缓存多次调用
\u myFunction
:
Before the `dom-repeat`:
execute the function
cache the result in an object
In the `dom-repeat`
use the cached-object to retrieve the value
好处:使用备忘录也可以工作在你的\u My函数中提供代码?我不明白为什么它应该更改任何东西在你的\u My函数中提供代码?我不明白为什么它应该更改任何东西是的,这会工作。它部分地回答了我的问题。我知道我想要的是通过在
dom repeat
Yes之前调用函数来缓存函数的结果。当然,这是可行的。它部分地回答了我的问题。我知道我想要的是通过在dom repeat