我正在Backbone.Marionette上建立一个集合,大量基于David Sulc在他的书“A Terntle Introduction to 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"}]
非常感谢任何帮助。
答案 0 :(得分:5)
我明白了。 看起来像一个Backbone bug。
将项添加到集合时,它调用Collection.set,它首先将所有获取的元素放入名为toAdd的数组中。然后它从toAdd集合添加到内部模型集合,但随后它会触发toAdd集合上的事件!因此,“添加”事件的触发顺序与从服务器接收的顺序相同。
https://github.com/jashkenas/backbone/blob/master/backbone.js#L733
Marionette挂钩到“add”事件,因此它可以呈现元素,因此它们的呈现方式与toAdd集合的顺序相同,即从服务器接收的顺序。
因此,要解决此问题,您可以在fetch调用的选项中传递{reset:true}: http://backbonejs.org/#Collection-fetch
谢谢, 鲍里斯
编辑:我不认为这是一个Backbone错误,我认为这是出于性能的目的。
答案 1 :(得分:1)
不确定是否只将模型字段名称作为比较器来帮助。相反,您可以在比较器中编写自定义函数,如下所示:
comparator : function (m1, m2) {
var str1, str2;
str1 = m1.get('firstName');
str2 = m2.get('firstName');
if (str1 && str2) {
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1 > str2) {
return 1;
} else if(str2 > str1) {
return -1;
}
}
}
每次向集合添加/删除模型或调用集合sort
方法