【问题标题】:Backbone.js collection fetch not setting response objects as modelsBackbone.js 集合获取未将响应对象设置为模型
【发布时间】:2015-06-21 20:19:54
【问题描述】:

当获取一个集合时,我的 api 响应有 10 个对象,但是生成的 Backbone 集合只有 1 个模型,其属性数组包含 10 个响应对象....换句话说,获取不是创建模型我的回复中的对象......我不知道为什么。

模型定义:

MyApp.Item = Backbone.Model.extend({
    initialize: function(){

    }
});

集合定义:

MyApp.ItemCollection = Backbone.Collection.extend({
    model: MyApp.Item,
    url: '/api/v1/item/',
    parse : function(response){
        //api returns objects in the content attribute of response, need to override parse
        return response.content;  
    }
});

调用 fetch:

var myCollection = new MyApp.ItemCollection();

myCollection.fetch({
    traditional: true,
    data: {  //url params for api call
        u_id: currentUser.id,
        order: 'sort_date:desc',
        start: 0,
        num_items: 10,
        format:'json'}
    });

结果:

console.log(response.content);

4571221007823F95BAAFB2BDF81111XX: Object
0124207763051005AAF59694458EBFXX: Object
3324207755431003B589CEF237DBE1XX: Object
3470000061641005BFB5D9983156E0XX: Object
3515553061641005A02884677F5624XX: Object
3526033426761006AFEA9852B0DDB5XX: Object
21431252714010079E4D8413429DB0XX: Object
26570547220410068F60D1B07D2E08XX: Object
37557124663710079DDC81EE855981XX: Object
0152243312031007957B94F5073B69XX: Object

//api successfully returns an array of objects, with GUID as key

console.log(myCollection);

r {length: 1, models: Array[1], _byId: Object}

//why only one model? why not 10?

console.log(myCollection.models[0].attributes);

4571221007823F95BAAFB2BDF81111XX: Object
0124207763051005AAF59694458EBFXX: Object
3324207755431003B589CEF237DBE1XX: Object
3470000061641005BFB5D9983156E0XX: Object
3515553061641005A02884677F5624XX: Object
3526033426761006AFEA9852B0DDB5XX: Object
21431252714010079E4D8413429DB0XX: Object
26570547220410068F60D1B07D2E08XX: Object
37557124663710079DDC81EE855981XX: Object
0152243312031007957B94F5073B69XX: Object
__proto__: Object

//there they are...but why?  

如何更改 fetch 以将这些对象作为单独的模型添加到 myCollection 中?

【问题讨论】:

    标签: backbone.js collections fetch


    【解决方案1】:

    响应必须是一个数组。数组中的每一项都变成了一个模型。您的响应是一个对象。

    更新您的 parse 方法以将值(作为数组)从响应中提取到数组中,它会起作用。

    MyApp.ItemCollection = Backbone.Collection.extend({
        model: MyApp.Item,
        url: '/api/v1/item/',
        parse : function(response){
            //api returns objects in the content attribute of response, need to override parse
            return _.map(response.content, function(model, id) {
                model.id = id;
                return model;
            });
        }
    });
    

    【讨论】:

    • 科里,你是一位绅士和一位学者。那行得通。有关如何将模型的 id 属性设置为响应中的 GUID 的任何指导?
    • 是的,我感觉你需要这样做。 1 秒,让我编辑帖子
    • models 数组的数组键是否总是 Backbone 集合中的整数索引?我习惯于使用 GUID 覆盖数组键以便于访问...在 Backbone 中这种形式不好吗?
    • 不,ID 可以是任何东西。字符串、guid、数字,无关紧要——它必须是唯一的。
    猜你喜欢
    • 2012-07-08
    • 1970-01-01
    • 2013-07-10
    • 2011-11-30
    • 1970-01-01
    • 2015-12-07
    • 2011-10-31
    • 2013-08-29
    • 1970-01-01
    相关资源
    最近更新 更多