Backbone fetch返回5个模型但是当我渲染我的集合视图时,只存在1个模型?

时间:2014-07-03 21:32:51

标签: javascript backbone.js

我真的不确定我做错了什么但是当我创建fetch并记录响应时我可以看到5个对象,但是,当我在视图中注销数据时,我在监听集合同步事件时,只有1个对象被注销?我的数据存储在global dataStore object dataStore.videos,任何人都可以解释我可能出错的地方?

VideoSearchCollection.js

getVideos: function(searchTerm) {

            this.searchTerm = searchTerm;

            this.fetch();
        },
parse: function(response) {

            console.log('this is response.items: ', response.items); // logs 5

            return response.items;
        }

SearchResultsView.js

initialize: function() {

            // listens to a change in the collection by the sync event and calls the render method
            this.listenTo(dataStore.videos, 'sync', this.render);

            console.log('This collection should look like this: ', dataStore.videos);
        },


        render: function() {

            console.log('inside render', dataStore.videos); // logs 1??

            var self = this,
                gridFragment = this.createItems();

                this.$el.html(gridFragment);

            return this;
        },

我的数据 enter image description here

1 个答案:

答案 0 :(得分:1)

这很重要。看起来你的服务器响应正在返回没有id的项目,所以Backbone将所有这些项合并为一个。

错误的服务器响应:

{"name","dan 1"}
{"name","dan 2"}

正确的服务器响应:

{"name","dan 1", id: 1}
{"name","dan 2", id: 2}