2017-01-12 9 views
0

レンダリング時にハンドルバーテンプレートに変数を渡す方法はありますか?私は、このテンプレートがあります。テンプレートをレンダリングするときにハンドルバーに変数を渡す

<script id="listingTopics" type="text/x-handlebars-template"> 
{{#each this}} 
    <div class="wrapper_individual_listing_topic wrapper_form_dark"> 
     <div class=" form_section"> 
      <div class="topics_titles wrapper_listing_topics"> 
       <a href="topic-{{id}}" class="listing-topics">{{title}}</a> <svg class="affinity {{heartClass}}" data-topicid="{{id}}"><use xlink:href="#starEmpty"/></svg> 
      </div> 

      <div class="topic-description hide topic-{{id}}"> 
       <p class="text_standard">{{body}}</p> 
      </div> 
     </div> 
    </div> 
{{/each}}</script> 

をそして私は、私はテンプレートをレンダリングするときに設定したい変数がこれを行うには、私の最初のアイデアは、機能を設定することでした

heartclassです:

function showTopics(container, data, heartclass) { 
    var heartClass = heartclass 
    var listingTopicsCompiled = listingTopicsTemplate(data); 
    $(container).append(listingTopicsCompiled); 
} 

このような関数を呼び出してください。

showTopics('#listing-topics', not_favorite_exchanges, 'unfav'); 

これをどのように攻撃するかわかりません。どんな助けでも大歓迎です!

答えて

0

foreachループを追加し、キー値のペアを配列の各オブジェクトに追加するだけでした。だからこれはこれです:

function showTopics(container, data, heartclass) { 
    data.forEach(function(entry) { 
     entry.heartClass = heartclass; 
    }); 
    var heartClass = heartclass 
    var listingTopicsCompiled = listingTopicsTemplate(data); 
    $(container).html(listingTopicsCompiled); 
} 

しかし、私はより速く/何らかの方法で静的変数にアクセスすると思います。

関連する問題