2011-05-11 47 views
1

我有:jquery1.6.min,jquery-tmpl.js(最新测试版)和knockout-1.2.0.js。我使用了一个非常简单的例子,但我无法获取jQuery模板来渲染,我找不到原因,简单地说,我可以通过使用萤火虫扫描dom来查看dom中的元素 - 我有一些tmp = annonymous(jquery,$ item )出现,但数据不在dom内呈现。开始与jQuery模板问题

模板:

<script id="bookTemplate" type="text/x-jquery-tmpl"> 
    <h2>${title}</h2> 
    price: ${formatPrice(price)} 
</script> 

jscode:

<script type="text/javascript"> 
     $(document).ready(function() { 
     // Create an array of books 
     var books = [ 
      { title: "ASP.NET 4 Unleashed", price: 37.79 }, 
      { title: "ASP.NET MVC Unleashed", price: 44.99 }, 
      { title: "ASP.NET Kick Start", price: 4.00 }, 
      { title: "ASP.NET MVC Unleashed iPhone", price: 44.99}]; 

     function formatPrice(price) { 
      return "$" + price.toFixed(2); 
     } 

     // Render the books using the template 
     $('#bookTemplate').tmpl(books).appendTo('#bookContainer'); 
     }); 
    </script> 

回答

0
window.formatPrice = function formatPrice(price) { 
    return "$" + price.toFixed(2); 
} 

Live Example

你调用模板formatPrice但在你关闭定义的功能。为了能够做到这一点,formatPrice必须在全球范围内。一个简单的方法来做到这一点是分配功能windiw.formatPrice

+0

我觉得自己像一个完全白痴......你显示我帮助了我,但我错过了添加我追加的实际容器:'

' – Haroon 2011-05-11 16:15:17

+0

@哈龙哦。我认为你有你的HTML:D如果它解决了你的问题接受它:) – Raynos 2011-05-11 16:19:09

+0

我不得不等待4分钟出于某种原因,会做。谢谢 :) – Haroon 2011-05-11 16:20:02