Подключаемый модуль Jquery Templates - PullRequest
2 голосов
/ 16 ноября 2010

Эй, ребята, я пробую этот пример из блога Дэйва Уорда

о шаблонах jQuery и о том, что я делаю неправильно.Любая помощь будет оценена.Вот его код: Данные:

 var invoice = {
  invoiceItems: [
    { type: 'item', 
      part: '99Designs', description: '99 Designs Logo', 
      price: 450.00, qty: 1 },
    { type: 'service',
      service: 'Web development and testing', 
      price: 25000.00 },
    { type: 'item',
      part: 'LinodeMonthly', description: 'Monthly site hosting', 
      price: 40.00, qty: 12 }
  ]
};

Клиент:

  <script id="invoiceTemplate" type="x-jquery-tmpl">
      <table class="invoice">
      {{each lineItems}}
        {{tmpl($value) get_invoiceRowTemplateName(type)}}
      {{/each}}
      </table>
  </script>

Js:

$(function ()
{
    $('#invoiceTemplate').tmpl(invoice).appendTo('body');
});

function get_invoiceRowTemplateName(type) {
  // Return a template selector that matches our 
  //  convention of <type>RowTemplate.
  return '#' + type + 'RowTemplate';
}

1 Ответ

3 голосов
/ 16 ноября 2010

Не забудьте шаблоны строк:

<script id="serviceRowTemplate" type="x-jquery-tmpl">
  <tr class="service">
    <td colspan="2">${service}</td>
    <td colspan="2">${price}</td>
  </tr>
</script>

<script id="itemRowTemplate" type="x-jquery-tmpl">
  <tr class="item">
    <td>${item}</td>
    <td>${description}</td>
    <td>${price}</td>
    <td>${qty}</td>
  </tr>
</script>

Когда get_invoiceRowTemplateName () разрешает type каждого элемента в соответствующий * тип * RowTemplate *, эти отдельные шаблоны строк используются для визуализации каждого элемента..

...