2012-07-09 84 views
0

我在Backbonejs一個簡單的添加事件,我也有模板:骨幹underscorejs增值模板

initialize: function() 
{ 
    this.template = _.template($("#person-template").html()); 
} 

renderperson: function(model) 
{ 
    //model.name is "Jack" for example 
    //Now I want to render this template with Name replaced with "Jack"? 
    $("#somelement").append(); //What to do here??? 
} 

模板很簡單:

<script type="text/template" id="person-template"> 
    <div class="person"><%= Name %></div> 
</script> 

回答

2
$("#somelement").append(this.template(this.model.toJSON())); 

您可能還需要添加

_.bindAll(this) 

給你的初始化方法

+0

'this.template' 不是一個功能 – user50992 2012-07-09 14:18:51

+0

你加_.bindAll(this)來初始化功能? – 2012-07-09 14:20:16

+0

哦,我沒有添加它來初始化函數,我把它添加到渲染人。現在它可以工作。對不起,謝謝!:) – user50992 2012-07-09 14:21:51

0

可能是這可以幫助你: http://backbonejs.org/#View-render

var Bookmark = Backbone.View.extend({ 
    render: function() { 
    $(this.el).html(this.template(this.model.toJSON())); 
    return this; 
    } 
});