Javascript 主干集合未完全从JSON填充

Javascript 主干集合未完全从JSON填充,javascript,json,backbone.js,Javascript,Json,Backbone.js,我正在尝试从JSON数据填充主干集合。然而,在我尝试填充集合后,我只得到其中一个项目,而我应该得到12个。你知道发生了什么事吗 JS: JSON: HTML: 您的数据是一个对象,而不是一个数组,这正是主干集合所期望的 您有两个选择: 将数据更改为数组 向集合添加一个从结构中获取所需数组的方法: Backbone.Collection.extend({ 解析:函数(有效负载){ 返回有效载荷 } }) (function ($) { var Sponsor = Backbone.

我正在尝试从JSON数据填充主干集合。然而,在我尝试填充集合后,我只得到其中一个项目,而我应该得到12个。你知道发生了什么事吗

JS:

JSON:

HTML:


您的数据是一个对象,而不是一个数组,这正是主干集合所期望的

您有两个选择:

  • 将数据更改为数组
  • 向集合添加一个从结构中获取所需数组的方法:
  • Backbone.Collection.extend({
    解析:函数(有效负载){
    返回有效载荷
    }
    })
    
    (function ($) {
    
    
        var Sponsor = Backbone.Model.extend({
            defaults: {
                link: "http://www.google.com",
                photo: "http://placekitten.com/g/500/500"
            }
        });
    
        var Sponsors = Backbone.Collection.extend({
            model:  Sponsor,
            url: '../json/sponsors.json'
        });
    
        var SponsorView = Backbone.View.extend({
            tagName: "li",
            className: "sponsors-container",
            template: $("#sponsorTemplate").html(),
            render: function() {
                var tmpl = _.template(this.template);
    
                this.$el.html(tmpl(this.model.toJSON()));
                return this;
            }
        });
    
        var SponsorsView = Backbone.View.extend({
            el: $(".sponsors"),
    
            initialize: function() {
                this.collection = new Sponsors();
                this.collection.on("sync", this.render, this);
                this.collection.fetch();
            },
    
            render: function() {
                var that = this;
                _.each(this.collection.models, function(item) {
                    that.renderSponsor(item)
                }, this);
            },
    
            renderSponsor: function(item) {
                var sponsorView = new SponsorView({
                    model: item
                });
                this.$el.append(sponsorView.render().el);
            }
        });
        var sponsor = new SponsorsView();
    } (jQuery));
    
    {
        "sponsors":[
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"}, 
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"},
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"},
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"}, 
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"},
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"},
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"}, 
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"},
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"},
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"}, 
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"},
            {"link":"http://google.com", "photo":"http://placekitten.com/g/500/500"}
        ]
    }
    
    <ul class="sponsors"></ul>
    <script id="sponsorTemplate"type="text/template">
    
                <a href="<%= link %>"><img src="<%= photo %>" /></a>
    
        </script>