我試圖實現通過使用模型的屬性作爲視圖中的變量來呈現HTML塊。Backbone.js視圖屬性中的變量
App = Backbone.Model.extend({
contentType: "text"
});
AppView = Backbone.View.extend({
ContentTpl: _.template($('#slide-'+this.model.get('contentType')).html()),
render: function(){
$('#wrapper').html(this.ContentTpl());
}
});
var app = new App();
var appView = new AppView({model: app});
appView.render();
HTML:
<div id="wrapper">
</div>
<script type="text/template" id="slide-text">
<p>This is a test</p>
</scrip>
但是,這會導致錯誤......
你得到了什麼錯誤? –
未捕獲TypeError:無法調用未定義的方法「get」 – FLuttenb