gpt4 book ai didi

backbone.js - 使用 Restful 服务的集合排序不适用于 Backbone.Marionette

转载 作者:行者123 更新时间:2023-12-04 02:54:21 24 4
gpt4 key购买 nike

我正在 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>

rest get返回的Json Array为

[{"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"}]

如有任何帮助,我们将不胜感激。

最佳答案

我想通了。 看起来像一个 Backbone 错误。当您将项目添加到集合时,它会调用 Collection.set,它首先将所有获取的元素放入一个名为 toAdd 的数组中。然后它从 toAdd 集合添加到内部模型集合,然后触发 toAdd 集合上的事件!因此,“添加”事件的触发顺序与您从服务器接收到它们的顺序相同。 https://github.com/jashkenas/backbone/blob/master/backbone.js#L733

Marionette Hook 到“添加”事件,因此它可以呈现元素,因此它们以与 toAdd 集合相同的顺序呈现,即您从服务器接收到的顺序。

因此,要解决此问题,您可以在 fetch 调用的选项中传递 {reset: true} : http://backbonejs.org/#Collection-fetch

谢谢,鲍里斯

编辑:我不认为这是一个 Backbone 错误,我认为这是为了提高性能。

关于backbone.js - 使用 Restful 服务的集合排序不适用于 Backbone.Marionette,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18095389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com