how to add json to backbone,js collection using fetch

后端 未结 2 1939
轻奢々
轻奢々 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:49

    If you use rest api, try turn off these parameters:

    Backbone.emulateHTTP Backbone.emulateJSON

    0 讨论(0)
  • 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"
            }
        ]
    
    0 讨论(0)
提交回复
热议问题