2014-03-12 52 views
0

Helllo,這可能是一個常見問題,但我沒有找到合適的答案。在Backbone中使用模板

我有一個HTML文件:header.html中包含我要顯示的標題

這裏是我的Backbone.View

el: $(".header"), 

initialize: function() { 
    this.render(); 
}, 

render: function() { 
    var template = _.template($("#header_template").html(), {}); 
    this.$el.html(template); 
}, 

當我把代碼的Java腳本模板內,它的工作原理:

<script type="text/template" id="header_template"> 
    code of header.html goes here 
</script> 

但是,當我使用這種方式:

<script type="text/template" id="about_template" src="header.html"></script> 

即使Firebug看到模板內的代碼,它也會停止工作。

有人能告訴我有什麼錯誤,以及如何解決它

+0

這可能會有所幫助:http://stackoverflow.com/questions/8366733/external-template-in-underscore –

+0

所以我們要麼把代碼中的變量或使用GET方法? – user3412802

+0

有沒有更優雅的解決方案? – user3412802

回答

0

一個乾淨的方式來組織你的模板是創建一個子文件夾tpl。在這裏您添加所有的.html文件

Coenraets有a nice tutorial他將這種方法與templateloader函數一起使用。

然後,在引導骨幹應用程序之前,您將模板加載到內存中。

tpl.loadTemplates(['header', 'wine-details', 'wine-list-item'], function() { 
    app = new AppRouter(); 
    Backbone.history.start(); 
}); 
相關問題