我剛開始學習backbone.js,遇到了我的第一個問題。我想用attributes
財產User
視圖注入的屬性,而不是在我的模板本身寫他們的:Backbone.js在使用模板時無法合併屬性?
<div id="lista"></div>
<script type="text/template" id="user-template">
<a class="btn" href="#"><i class="icon-user"></i>
<%= first %> <%= last %></a>
</script>
window.UserView = Backbone.View.extend({
attributes : { "data-route" : '/users/' + this.name },
template: _.template($('#user-template').html()),
initialize : function() {
_.bindAll(this, "render");
},
render : function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
var u = new User({ first : 'First', last : 'Last' });
var v = new UserView({ model : u, el : '#lista' });
v.render();
輸出缺少data-route
屬性:
<div id="lista">
<a href="#" class="btn"><i class="icon-user"></i> First Last</a>
</div>
你能否證實attributes
不適用於template
?