HOME > > > >

.tmplItem()

テンプレートをレンダリングして、ドキュメントに挿入したDOMエレメントの情報を取得します?

返ってくるオブジェクトは下記のプロパティを持ってる。

data
テンプレートレンダリング時に使用したデータ
nodes
ノード
parent
テンプレートがネストされてた場合、親テンプレートが設定される。
tmpl
テンプレートとして利用した
レンダリング時にバインドしたデータ

サンプル

実行結果

値段:

Javascript

  1. $('#template-1').tmpl([
  2.         { no: 1, name: 'ロケット',   price: 100},
  3.         { no: 2, name: 'ランチャー', price: 200},
  4.         { no: 3, name: 'ドリル',     price: 300},
  5.         { no: 4, name: 'レーダー',   price: 400}
  6.     ], {
  7.         tax: function(price){
  8.                  return price * 1.05
  9.              }
  10.     })
  11.     .appendTo('.switches');
  12.  
  13. $('.switches :radio').on('click', function(e){
  14.  
  15.     var item = $(e.target).tmplItem();
  16.     $('.jqTest1 p span').text(item.tax(item.data.price));
  17.  
  18.     $(item.nodes).css({
  19.         display: 'inline-block',
  20.         padding: 10,
  21.         fontWeight: 'bold'
  22.     });
  23. });
  24.  

HTML

  1. <div class="jqTest1">
  2.  
  3.     <script id="template-1" type="text/x-jquery-tmpl">
  4.         <label><input type="radio" name="switch" value="${no}" />${name}</label><br />
  5.     </script>
  6.  
  7.     <div class="switches">
  8.     </div>
  9.  
  10.     <p>値段:<span></span></p>
  11. </div>