how to add json to backbone,js collection using fetch

后端 未结 2 1952
轻奢々
轻奢々 2021-02-15 15:23

I am trying to get backbone.js to load json. The json loads but i am not sure how to get the items into my collection. Or maybe that happens automatically and i just can\'t trac

2条回答
  •  一个人的身影
    2021-02-15 15:59

    Your json is not in the correct format, you can fix the json or add a hint to backbone in the parse method:

    var ClientCollection = Backbone.Collection.extend({
        defaults: {
            model: Client
        },
        model: Client,
        url: 'json/client.json',
    
        parse: function(response){
           return response.items;
        }
    });
    

    Or fix your JSON:

     [
            {
                "name": "WTBS",
                "img": "no image"
            },
    
            {
                "name": "XYC",
                "img": "no image"
            }
        ]
    

提交回复
热议问题