主要成分:从服务器上获取集合 [英] 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: { ... }, ...}
然而误差总是相同的。
有什么不对这个code?
However the error was always the same. What is wrong with this code?
它不利于通过设置 coll.fetch({错误:errorFunc})的错误;
除外住宿相同。
It doesn't help to set an error via coll.fetch({error: errorFunc});
The Exception stays the same.
好了,似乎一切都很正常,直到 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.
的错误是在 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"}]
这篇关于主要成分:从服务器上获取集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!