2012-10-05 36 views
0

我對Backbone.js和Require.js很新穎。在我的應用我加載模板到每個模塊通過需要(使用文本插件!),只是如下:將數據插入到加載了require.js的模板中

define([ 
'jQuery', 
'Underscore', 
'Backbone', 
'API', 
'Utils', 
'text!templates/home/register.html' 
], function($, _, Backbone, api, utils, registerTpl){ 
     var registerView = Backbone.View.extend({ 
      el: $("#content"), 
      render: function(){ 
       this.el.html(registerTpl); 
      }, 
      {...} 

我不知道如何將數據綁定模型或直接將數據加載到我的模板作爲在backbonetutorials.com實施例所示,是這樣的:

{...} 
render: function(){ 
     //Pass variables in using Underscore.js Template 
     var variables = { search_label: "My Search" }; 
     // Compile the template using underscore 
     var template = _.template($("#search_template").html(), variables); 
     // Load the compiled HTML into the Backbone "el" 
     this.el.html(template); 
}, 
{...} 
<script type="text/template" id="search_template"> 
<!-- Access template variables with <%= %> --> 
<label><%= search_label %></label> 
<input type="text" id="search_input" /> 
<input type="button" id="search_button" value="Search" /> 
</script> 

任何瞭解,尖端或代碼片段將被理解的。

回答

3

那麼這很簡單,在教程中,它直接從DOM獲取模板數據,而您將它作爲參考傳遞給require js。

你可以做這樣的事情:

template = _.template(registerTpl), 

render: function(){ 
     var variables = { search_label: "My Search" }; 
     this.el.html(this.template(variables)); 
     return this; 
}, 

相反,如果你想與你的模板中使用模型的數據:

this.el.html(this.template(this.model.toJSON()));