我试图渲染使用下划线模板引擎的HTML表。 首先我有这样的服务器JSON响应
{
CurrentModel: {
Heading: "Home",
Id: "pages/193",
Metadata: {
Name: "Home",
Title: null,
Keywords: null,
Description: null,
DisplayInMenu: true,
Published: "/Date(1334499539404)/",
Changed: "/Date(1334499539404)/",
ChangedBy: "Marcus",
IsPublished: true,
IsDeleted: false,
Slug: null,
Url: null,
SortOrder: 0
},
Parent: null,
Children: [
"pages/226",
"pages/257"
]
},
Children: [
{
Heading: "Foo",
MainBody: null,
Id: "pages/226",
Metadata: {
Name: "I'm an article",
Title: null,
Keywords: null,
Description: null,
DisplayInMenu: true,
Published: "/Date(1334511318838)/",
Changed: "/Date(1334511318838)/",
ChangedBy: "Marcus",
IsPublished: true,
IsDeleted: false,
Slug: "i-m-an-article",
Url: "i-m-an-article",
SortOrder: 1
},
Parent: {},
Children: [ ]
},
{
Heading: "Bar",
MainBody: null,
Id: "pages/257",
Metadata: {
Name: "Foo",
Title: null,
Keywords: null,
Description: null,
DisplayInMenu: true,
Published: "/Date(1334953500167)/",
Changed: "/Date(1334953500167)/",
ChangedBy: "Marcus",
IsPublished: true,
IsDeleted: false,
Slug: "foo",
Url: "foo",
SortOrder: 2
},
Parent: {},
Children: [ ]
}
]
}
HTML结果我要找的是非常喜欢这个地方我打印了一些来自CurrentModel并通过儿童属性迭代,最好在该TBODY每个TR中的数据应该使用Backbone.js的是一个视图,以便我能线一些事件这个特定的行。
<table>
<caption><%= CurrentModel.Metadata.Name %></caption>
<thead>
<tr>
<th><span>Page name</span></th>
<th><span>Slug</span></th>
<th><span>Published</span></th>
<th><span>Changed</span></th>
</tr>
</thead>
<tbody>
<% _(Children).each(function(page) { %>
<tr>
<td><%= page.Metadata.Name %></td>
<td><%= page.Metadata.Slug %></td>
<td><%= page.Metadata.IsPublished %></td>
<td><%= page.Metadata.Changed %></td>
</tr>
<% }); %>
</tbody>
</table>
现在,我的问题是如何使我的骨干视图和模型的样子,或者它并不意味着这样被使用? 以下作品的使用JavasScript代码,并呈现每个儿童一TR如果从服务器的响应就是一个页面的集合,我理解的方式,但我不知道如何修改代码,以便它可以采取一个复杂的模型,然后渲染部分在一个或者两个JavaScript的观点,即模型?
在我的application.js我有这样的代码
pages: function () {
this.pageList = new PageCollection();
this.pageListView = new PageListView({ model: this.pageList });
this.pageList.fetch();
}
其中PageCollection看起来是这样的
var PageCollection = Backbone.Collection.extend({
url: '/pages',
model: Page
});
模型类这样
Page = Backbone.Model.extend({
metadata: {
name: null,
title: null,
keywords: null,
description: null,
displayInMenu: null,
published: null,
changed: null,
changedBy: null,
isPublished: null,
isDeleted: null,
slug: null,
url: null,
sortOrder: null
},
parent: {},
children: [],
ancestors: null,
initialize: function () { }
});
该PageListView
PageListView = Backbone.View.extend({
tagName: 'table',
initialize: function () {
this.model.bind("reset", this.render, this);
},
render: function (eventName) {
_.each(this.model.models, function (page) {
$(this.el).append(new PageListItemView({ model: page }).render().el);
}, this);
return this;
}
});
并在最后PageListItemView
PageListItemView = Backbone.View.extend({
tagName: "tr",
template: _.template($('#tpl-page-list-item').html()),
events: {
"click td input[type=checkbox]": "publish"
},
render: function (eventName) {
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
publish: function (event) {
alert('Publish');
}
});