我正在建立一个关于 Backbone.Marionette 的集合,主要基于 David Sulc 在他的书“Backbone.Marionette 的温和介绍”中提供的示例,可在此处获取https://github.com/davidsulc/marionette-gentle-introduction/commit /175fc9b7bddfa6fea86954eb769c0cfb3e163c1e。
目前我仍在内联做所有事情:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Marionette Contact Manager</title>
<link href="./css/bootstrap.css" rel="stylesheet">
<link href="./css/application.css" rel="stylesheet">
<link href="./css/jquery-ui-1.10.0.custom.css" rel="stylesheet">
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<span class="brand">Secret Identities</span>
</div>
</div>
</div>
<div id="main-region" class="container">
//main area
</div>
<script type="text/template" id="contact-list-item">
<td> <%= lastName %></td><td> <%= firstName %> </td><td> <%= occupation %> </td>
</script>
<script type="text/template" id="contact-list">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Occupation</th>
</tr>
</thead>
<tbody>
</tbody>
</script>
<script src="./js/vendor/jquery.js"></script>
<script src="./js/vendor/json2.js"></script>
<script src="./js/vendor/underscore.js"></script>
<script src="./js/vendor/backbone.js"></script>
<script src="./js/vendor/backbone.marionette.js"></script>
<script type="text/javascript">
var Application = new Marionette.Application();
Application.addRegions({
mainRegion: "#main-region"
});
Application.Contact = Backbone.Model.extend({
urlRoot: "/rest/example/listHeroes"
});
Application.ContactCollection = Backbone.Collection.extend({
model: Application.Contact,
url: "/rest/example/listHeroes",
comparator: "firstName"
});
Application.ContactItemView = Marionette.ItemView.extend({
tagName: "tr",
template: "#contact-list-item"
});
Application.ContactsView = Marionette.CompositeView.extend({
tagName: "table",
className: "table table-hover",
template: "#contact-list",
itemView: Application.ContactItemView,
itemViewContainer: "tbody"
});
Application.on("initialize:after", function () {
var list = new Application.ContactCollection;
list.fetch();
var contactsView = new Application.ContactsView({
collection: list
});
Application.mainRegion.show(contactsView);
});
Application.start();
</script>
</body>
</html>
</body>
</html>
其余 get 返回的 Json 数组是
[{"firstName":"Bruce","lastName":"Wayne","occupation":"Industrialist"},{"firstName":"Steve","lastName":"Rogers","occupation":"Soldier"},{"firstName":"Natasha","lastName":"Romanov","occupation":"spy"},{"firstName":"Clark","lastName":"Kent","occupation":"Reporter"},{"firstName":"Hal","lastName":"Jordan","occupation":"Pilot"}]
任何帮助将不胜感激。