Backbone.js Collection'的fetch调用出错
Backbone.js Collection's fetch call is erroring out
仍然围绕着Backbone,并且遇到了一个问题,当我从文件传递JSON时,我的Collection不会填充。fetch()调用出错了,但我不确定如何准确地调试它,因为我的console.log()没有告诉我任何有用的东西(据我所知如何挖掘它们)。
代码如下:
ItemGroup = Backbone.Collection.extend({
model: Item, // left out definition since it's very simple
url: 'js/items.json',
initialize: function(models, options) {
this._meta = {};
},
parse: function(response) { // parse is not being called, not sure why?
this.set(response.name, 'name');
return response.items;
},
set: function(val, prop) {
this._meta[prop] = val;
},
get: function(prop) {
return this._meta[prop];
}
});
ItemGroupView = Backbone.View.extend({
el: '#item-container',
initialize: function(options) {
this.collection.bind('reset', this.render, this);
this.collection.fetch({
success : function() {
alert('successful');
},
error : function(collection, xhr, options) { // This is being triggered, not sure why?
console.log(collection);
console.log(xhr);
console.log(options);
}
});
},
processItem: function(item) {
var itemView = new ItemView({ model: item });
this.$el.append(itemView.render().el);
},
render: function() {
console.log(this.collection); // this shows an empty collection
var itemGroupHtml = this.template({ name: this.collection.get('name') });
$(this.main).html(itemGroupHtml);
_.each(this.collection.models, this.processItem, this);
return this;
}
});
var toiletryItems = new ItemGroup();
var toiletryGroupView = new ItemGroupView({ collection: toiletryItems });
这是我的json(我可以看到它成功地找到了文件,因为我看到它作为一个200在网络请求使用chrome检查器):
[
{
'name' : 'toiletries',
'items' : [
{ 'name': 'toothbrush' },
{ 'name': 'deodorant' },
{ 'name': 'toothpaste' },
]
}
]
提前感谢!
更新:
修复了无效的json,但仍然看到一个空的toiletryItems集合。什么好主意吗?
发现问题与我的解析函数。响应参数是一个数组,因此该函数应该是:
parse: function(response) { // parse is not being called, not sure why?
this.set(response[0].name, 'name');
return response[0].items;
},
相关文章:
- IE11中的第二个调用取消了第一个Fetch API调用
- Backbone fetch中的Ajax在fetch调用退出后完成,因此fetch调用中没有成功/失败事件
- 如何在fetch post调用后管理重定向请求
- 如何判断页面是否是从fetch()调用的
- fetch()不调用其Success和Error回调函数(backbone.js)
- 主干集合在调用 fetch() 时给出“未定义的 Ajax”错误
- 在不调用 fetch 的情况下使用解析
- Backbone JS Model Fetch在Fiddler中返回200,但调用错误方法
- 只需要在主干javascript fetch()调用完成后运行代码
- 为什么'fetch函数'自动调用'解析函数'在backbone.js集合上
- backbone.js collection-重复调用fetch以获取所有“;页面”;来自服务器
- 什么是缓存fetch调用的正确方法
- 每次get时主干调用fetch
- 在fetch api调用时未设置标头
- Babel将新的Request (fetch-api)转换为函数调用,导致Chrome出现错误
- 主干JS:在fetch调用中分配一个模型属性
- 这个fetch调用问题
- 主干-在我的控制器中从一个fetch调用填充多个模型
- 处理redux中fetch调用时的错误
- Backbone.js Collection'的fetch调用出错