2017-11-01 11 views
0

私は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", ......} 
} 

しかし、私は、私はすべての私の<div>秒で_myFunction(column, index)を呼んでいる瞬間のために、これを行うための正しい方法を見つけることができません。

関数を何回も呼び出さないようにするにはどうすればよいですか?

+0

を働くかもしれない使用して_myFunction内のコードで提供されるものですか。 –

+0

私はそれが何かを変えなければならない理由はありません – Kursion

答えて

0

を試してみてください。

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 

ボーナス:メモ化も

0

私はキャッシュを使用して_myFunctionを複数回呼び出さないようにする方法を発見し、これはあなたのために働くことを願っています、次のコード、DOMリピートを有するポリマーカスタム要素のその作業例で

<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> 
+0

もちろん、これは動作します。それは私の質問に部分的に答えます。私が望むのは、関数の結果を 'dom-repeat'の前に呼び出すことでキャッシュすることです。 – Kursion

関連する問題