2014-07-15 22 views
0

我想使用Ajax來加載包含多個Handlerbars模板的html頁面。我有一個調用,比如:從Ajax調用多個Handlerbars JS模板調用

$.ajax({ 
     url: optList.baseURL + "/HandlerBarTemplate.html", 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     cache: true, 
     async: false, 
     success: function (data) { 
      source = data; 
      $('#hbList').html(source);   
     }, 
     error: function (xhr, status, error) { 
      alert(error); 
     } 
    }); 

我在HTML模板是這樣的:

<script id="item-panel-template1" type="text/x-handlebars-template"> 

<script id="item-panel-template2" type="text/x-handlebars-template"> 

... 

我見過有一個文件中的個人模板的例子,但還沒有看到任何一個地方文件包含多個模板。

+0

這個例子看起來像你要求的? http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro –

+0

除非這些模板是動態創建的,爲什麼不預先編譯並加載它們? – pdoherty926

+0

凱西該帖子要求每個模板都在我想要避免的單個文件中。 – keitn

回答

0

得到這個工作,通過改變與模板的擴展名爲.txt

$.ajax({ 
     url: optList.baseURL + "/HandlerBarTemplate.txt", 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (data) { 
      source = data; 
      $('#hbList').html(source);   
     }, 
     error: function (xhr, status, error) { 
      alert(error); 
     } 
    }); 

我當時能夠使用模板,因爲如果他們在html是內聯。