我正在尝试使用下划线模板引擎呈现 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 打印一些数据并遍历 Children 属性,最好 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>
现在,我的问题是我的主干视图和模型应该是什么样子,或者它不应该这样使用?如果来自服务器的响应只是一个页面集合,下面的 javascript 代码可以工作并为每个孩子呈现一个 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 = 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');
}
});