我是骨干和牵线木偶的新手。 这是我目前的进展。
<!DOCTYPE html>
<html>
<head>
<title>Marionette composite view test</title>
<script src="./assets/libs/jquery.js"></script>
<script src="./assets/libs/bootstrap/js/bootstrap.js"></script>
<script src="./assets/libs/underscore.js"></script>
<script src="./assets/libs/backbone.js"></script>
<script src="./assets/libs/backbone.marionette.js"></script>
<link href="./assets/css/style.css" rel="stylesheet">
</head>
<body>
<div id="content"></div>
<script type="text/template" id="fDD">
<td><%= subtype %></td>
<td><%= acs_code %></td>
</script>
<script type="text/template" id="fD">
<caption><%= name %> ( <%= rollnum %> ), Project Code(<%= projectcode %>)</caption>
<thead>
<tr>
<th> Submission Type </th>
<th> Access number </th>
</tr>
</thead>
<tbody id="sub-Region">
</tbody>
</script>
<script type="text/javascript">
var data = {"name": "Mr XYZ", "rollnum": "53", "projectcode": "3526",
"submissions": [{"subtype": "A5", "acs_code": "5689-64123"},
{"subtype": "A8", "acs_code": "5689-64122"},
{"subtype": "D1", "acs_code": "5689-64122"},
{"subtype": "A5", "acs_code": "5689-64122"}]}
MyApp = new Backbone.Marionette.Application();
MyApp.addRegions({
mainRegion: "#content"
});
Submission = Backbone.Model.extend({});
Submissions = Backbone.Collection.extend({
model: Submission
});
SubmissionInfo = Backbone.Model.extend({});
MainInfo = Backbone.Collection.extend({
model: SubmissionInfo
});
FilingView = Backbone.Marionette.ItemView.extend({
tagName: "tr",
template: "#fDD"
});
TableView = Backbone.Marionette.CompositeView.extend({
tagName: "table",
itemView: FilingView,
template: "#fD",
itemViewContainer: "#sub-Region",
className: "table table-hover table-condensed"
});
AccordionView = Backbone.Marionette.CollectionView.extend({
itemView: TableView
});
MyApp.addInitializer(function(options){
var mainInfo = new MainInfo(options.data);
mainInfo.each(function(iinfo){
var ss = iinfo.get("submissions");
var sss = new Submissions({collection: ss});
iinfo.set("submissions", sss);
});
MyApp.mainRegion.show(new AccordionView({collection: mainInfo}));
});
MyApp.start({data: data});
</script>
</body>
</html>
我想渲染一个表格,在其中我将在其标题中显示一些信息,并将其他详细信息显示为行。但是只有字幕部分在缺少行时呈现。即使这没有任何错误。怎么解决这个?任何更好的方法来实现这个???
以上代码;我正在使用:
Backbone.js 1.0.0
MarionetteJS v1.0.3
jQuery JavaScript Library v1.9.1
Underscore.js 1.4.4
答案 0 :(得分:1)
存在一些基本错误:如何初始化集合,正确的主干对象构造函数等。以下是代码的工作版本。
<script type="text/template" id="fDD">
<td><%= subtype %></td>
<td><%= acs_code %></td>
</script>
<script type="text/template" id="fD">
<caption><%= name %> ( <%= rollnum %> ), Project Code(<%= projectcode %>)</caption>
<thead>
<tr>
<th> Submission Type </th>
<th> Access number </th>
</tr>
</thead>
<tbody id="sub-Region">
</tbody>
</script>
<script type="text/javascript">
var data = {"name": "Mr XYZ", "rollnum": "53", "projectcode": "3526",
"submissions": [{"subtype": "A5", "acs_code": "5689-64123"},
{"subtype": "A8", "acs_code": "5689-64122"},
{"subtype": "D1", "acs_code": "5689-64122"},
{"subtype": "A5", "acs_code": "5689-64122"}]};
MyApp = new Backbone.Marionette.Application();
MyApp.addRegions({
mainRegion: "#content"
});
var Submission = Backbone.Model.extend({});
var SubmissionCollection = Backbone.Collection.extend({
model: Submission
});
var Project = Backbone.Model.extend({});
var FilingView = Backbone.Marionette.ItemView.extend({
tagName: "tr",
template: "#fDD"
});
var TableView = Backbone.Marionette.CompositeView.extend({
tagName: "table",
itemView: FilingView,
template: "#fD",
itemViewContainer: "#sub-Region",
className: "table table-hover table-condensed"
});
MyApp.addInitializer(function(options){
var projectInfo = new Project(options.data);
var submissions = new SubmissionCollection(projectInfo.get("submissions"));
MyApp.mainRegion.show(new TableView({model: projectInfo, collection: submissions}));
});
MyApp.start({data: data});
</script>
</body>