Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 集合排序在主干上不起作用。使用restful服务的木偶_Backbone.js_Marionette_Restful Url - Fatal编程技术网

Backbone.js 集合排序在主干上不起作用。使用restful服务的木偶

Backbone.js 集合排序在主干上不起作用。使用restful服务的木偶,backbone.js,marionette,restful-url,Backbone.js,Marionette,Restful Url,我正在主干网上建立一个收藏。提线木偶主要基于David Sulc在其《主干网的温和介绍》一书中提供的例子 目前,我仍在做所有内联工作: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Marionette Contact Manager</title> <link href="./css/bootstra

我正在主干网上建立一个收藏。提线木偶主要基于David Sulc在其《主干网的温和介绍》一书中提供的例子

目前,我仍在做所有内联工作:

<!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>

任何帮助都将不胜感激。

不确定是否只将模型字段名作为比较符写入会有所帮助。相反,您可以在comparator中编写自定义函数,如下所示:

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;
        }
    }
}
这将在每次向集合添加/删除模型或调用集合排序方法时对集合进行排序。看起来像一只脊骨虫。 将项目添加到集合时,它将调用collection.set,它首先将所有获取的元素放入名为toAdd的数组中。然后它将从toAdd集合添加到内部models集合,但随后它会触发toAdd集合上的事件!因此,“添加”事件的触发顺序与从服务器接收事件的顺序相同。

木偶钩子到“add”事件,因此它可以呈现元素,因此它们以toAdd集合的相同顺序呈现,这是您从服务器接收到的顺序

因此,要解决此问题,可以在fetch调用的选项中传递{reset:true}:

谢谢, 鲍里斯


编辑:我不认为这是一个主干错误,我认为这是出于性能考虑。

看起来它工作得非常好。。您有
比较器:“firstName”
。。但渲染时,标签在视图中交换。所以你可能只是被弄糊涂了。。模板
标题
名字姓氏
固定fiddle我意识到标签在哪里切换,但那只是标题,列表中没有实际更改,基于任意一个字段。我甚至只在其上留下一个字段来进行检查。在我发布的FIDLE中效果很好看起来排序正确如果数据来自JSON数组,而不是来自restful函数如果我更改为静态列表,如var list=new Application.ContactCollection([{lastName:'Batman'},{lastName:“Captain'Merica”}、{lastName:'Black Widow'}、{lastName:'Superman'}、{lastName:'Green Lantern'}]),它工作得很好。这是在我为模型添加了一个默认字段并将比较器更改为lastName之后。我尝试了类似的方法,按照书中的说法,结果仍然是没有排序=/你能做
list.sort()
下面的
list.fetch();
,以及我提到的逻辑..我已经尝试过了,虽然直接传入JSON数组可以工作,但它不会从restful服务排序
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;
        }
    }
}