Backbone:从服务器获取集合 [英] Backbone: fetch collection from server
问题描述
我正在尝试从我的服务器获取一个集合.我使用的是 0.3.3 版(不是来自 github 的大师)但是我在这个异常中运行:
I'm trying to fetch a collection from my server. I'm using version 0.3.3 (not the master from github) However I am running in this exception:
Uncaught TypeError: Cannot use 'in' operator to search for 'id' in {id=MyId, active=true}
jQuery.jQuery.extend._Deferred.deferred.resolveWith (jquery.js:869)
done (jquery.js:6591)
jQuery.ajaxTransport.send.callback
这是我创建错误的方式:
This is the way I created the error:
var MyModel = Backbone.Model.extend();
var MyCollection = Backbone.Collection.extend({
url: '/api/collection',
model: MyModel
});
var coll = new MyCollection();
coll.fetch();
/api/collection 中的元素以 JSON 格式解析.我尝试以各种格式返回它们
The elements in /api/collection are parsed in JSON. I tried to return them in various formats
["Id1", "Id2", ... ]
[{id: "Id1, somethingElse: "..."}, ...]
{id1: { id1s content}, id2: { ... }, ...}
但是错误总是一样的.这段代码有什么问题?
However the error was always the same. What is wrong with this code?
[Edit] 通过 coll.fetch({error: errorFunc});
设置错误没有帮助,异常保持不变.
It doesn't help to set an error via coll.fetch({error: errorFunc});
The Exception stays the same.
[Edit2] 好吧,似乎一切正常,直到 collection.fetch()
使用响应对象调用 collection.refresh()
.我没有覆盖任何这些功能.
Well it seems everything works fine until collection.fetch()
calls collection.refresh()
with the response object. I have not overwritten any of these functions.
[Edit3] 错误出在 collection.add()
方法中,原因是我的元素是字符串列表...我的服务器发送了它们错了.
The error is in the collection.add()
method and the reason is that my elements are a list of strings... My server sent them wrong.
推荐答案
代替
["id1", "id2", ..., "idn"]
客户期望
[{"id": "id1"}, ... {"id": "idn"}]
这篇关于Backbone:从服务器获取集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!