【发布时间】:2015-10-27 17:06:13
【问题描述】:
我正在尝试嵌套使用把手模板的主干视图。
http://jsfiddle.net/6j4yodz6/6/
我的问题是我不知道如何使用模板,所以外部和内部视图都在使用模板。此外,现在它显示模板的 html:
< li > Title1 - Content #1< /li>
< li > Title2 - Content #2< /li>
< li > Title3 - Content #3< /li>
HTML:
<script type="text/html" id="ul-template">
< ul class = "outer" > < /ul>
</script>
<script type="text/html" id="li-template">
< li > {{attributes.title}} - {{attributes.content}}< /li>
</script>
JAVASCRIPT:
var documents = [
new Backbone.Model({
title: 'Title1',
content: 'Content #1'
}),
new Backbone.Model({
title: 'Title2',
content: 'Content #2'
}),
new Backbone.Model({
title: 'Title3',
content: 'Content #3'
})];
var ContentsView = Backbone.View.extend({
tagName: 'ul',
render: function () {
_(this.collection).each(function (document) {
//How do you use ul-template?
this.$el.append(new DocumentListView({
model: document
}).render().el);
}, this);
return this;
}
});
var DocumentListView = Backbone.View.extend({
tagName: 'li',
events: {
'click': function () {
console.log("clicked");
}
},
render: function () {
var source = $("#li-template").html();
var template = Handlebars.compile(source);
//This is using the template but is displaying the html.
this.$el.html(template(this.model));
return this;
}
});
$('body').html(new ContentsView({
collection: documents
}).render().el);
【问题讨论】:
标签: javascript backbone.js handlebars.js