gpt4 book ai didi

javascript - 从 RESTful 服务查看主干数据

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:23 26 4
gpt4 key购买 nike

我正在尝试查看从服务导入的集合,在客户端使用主干,并将 python/flask 作为服务。当我发出 GET 请求时,我得到以下数据:

{"entries": [{"Title": "my title 1", "Description": "My desc 1", "Info": "Info 1", "ids": 1}, {"Title": "my title 2", "Description": "My desc 2", "Info": "Info 2", "ids": 2}]}

但是即使我使用 fetch,这些条目也没有显示在我的页面上。这是我的 ListView 代码:

var app = app || {};

app.ContactListView = Backbone.View.extend({
el: '#contacts',

initialize: function () {

this.collection = new app.ContactList();
this.collection.fetch({reset: true});
this.render();
this.listenTo( this.collection, 'reset', this.render );
},


render: function () {
this.collection.each(function( item ){
this.renderContact( item );
}, this );
},

renderContact: function ( item ) {
var contactView = new app.ContactView({
model: item
});
this.$('#ContactTable').append( contactView.render().el );
}
});

可能是什么原因?

最佳答案

原因是因为您的集合需要一个模型数组作为响应,但您的服务正在返回条目下的数组。来自 documentation

parse is called by Backbone whenever a collection's models are returned by the server, in fetch. The function is passed the raw response object, and should return the array of model attributes to be added to the collection.

要解决这个问题,您只需覆盖 parse 即可。方法返回模型数组。

例如:

app.ContactList = Backbone.Collection.extend({
//...
parse: function (response) {
return response.entries;
}
})

关于javascript - 从 RESTful 服务查看主干数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23662117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com