3

我正在建立一个关于 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"}]

任何帮助将不胜感激。

4

2 回答 2

5

我想到了。看起来像一个 Backbone 错误。 当您将项目添加到集合中时,它会调用 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 错误,我认为这是为了提高性能。

于 2013-08-15T11:57:32.547 回答
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方法时,这都会对集合进行排序

于 2013-08-07T07:11:09.123 回答