【发布时间】:2015-10-27 16:04:37
【问题描述】:
当我实际使用console.log(this.collection.fetch()) 时,您可以在responceJSON 和responseText 中看到它从api 返回了正确的数据。
但是,当我运行时,我的 readyState 始终保持为 1
this.collection.fetch({
success : function() {
console.log(that.collection.toJSON())
}
});
即使我们没有服务器响应数据,它也会返回一个空白数组。
这是我的代码...
型号
define(['backbone'], function(backbone){
var google_place = Backbone.Model.extend({
defaults : {
'title' : 'no titile',
'sub_title' : 'no subtitle',
'content' : 'no content available',
'lat' : 0,
'lng' : 0,
'text' : 'no text'
}
});
return google_place;
});
收藏
define(['../model/google_place'], function(google_place){
var google_places = Backbone.Collection.extend({
model: google_places,
initialize: function(models, options) {
},
url: function() {
return "http://globallcoach-app.dev/api/";
},
parse: function(data) {
return data.items;
},
});
return google_places;
});
查看
define(includes(), function(backbone, mustache){
var index = Backbone.View.extend({
initialize : function() {
this.template = mustache.render(nav);
},
render : function() {
var that = this;
console.log(that.collection.fetch());
this.collection.fetch({
success : function() {
console.log(that.collection.toJSON())
},
});
return;
},
});
return index;
});
来自 fetch (responseText) 的 JSON 响应
[{
"title": "no titile",
"sub_title": "no subtitle",
"content": "no content available",
"lat": "0",
"lng": "0",
"text": "no text"
}, {
"title": "Example Title",
"sub_title": "Example Subtitle",
"content": "Loads on content here",
"lat": "0",
"lng": "0",
"text": "random text bullshit"
}]
【问题讨论】:
-
您看到的响应格式是什么?可以发一下json吗?您确定 google_places 中的解析功能是必要的吗?响应中真的有 items 数组吗?
-
我已经在回复中进行了编辑
-
看起来你不需要 parse 函数,因为响应 json 的顶层已经是一个 google_places 数组。
标签: javascript jquery rest backbone.js requirejs