Backbone:从服务器获取集合
我正在尝试从我的服务器获取集合。 我使用的是0.3.3版本(不是来自github的master) 但是我在这个异常中运行:
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
这是我创建错误的方式:
var MyModel = Backbone.Model.extend();
var MyCollection = Backbone.Collection.extend({
url: '/api/collection',
model: MyModel
});
var coll = new MyCollection();
coll.fetch();
/api/collection 中的元素以 JSON 进行解析。 我尝试以各种格式返回它们
["Id1", "Id2", ... ]
[{id: "Id1, somethingElse: "..."}, ...]
{id1: { id1s content}, id2: { ... }, ...}
但是错误总是相同的。 这段代码有什么问题?
[编辑] 通过 coll.fetch({error: errorFunc});
设置错误没有帮助,异常保持不变。
[Edit2] 看起来一切正常,直到 collection.fetch()
使用响应对象调用 collection.refresh()
。我没有覆盖任何这些函数。
[Edit3] 错误出现在 collection.add()
方法中,原因是我的元素是字符串列表...我的服务器发送了错误的它们。
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();
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] It doesn't help to set an error via coll.fetch({error: errorFunc});
The Exception stays the same.
[Edit2] 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] 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您已经确定您的响应格式不是 Backbone 所期望的,因此您应该重写 YourModel.parse 函数,该函数应该从服务器获取响应并返回集合可接受的模型数组。这是来自 Backbone.js 的片段
,您可以看到默认函数只是传递数据。您必须使其适合您的响应格式。
PS id 建议在 Backbone.fetch 方法中放置一个断点,以查看来自服务器的格式以及它到底在哪里中断了模型创建。
Since you already identified that your response format is not what Backbone expect, you should override YourModel.parse function that should take the response from your server and return an array of models acceptable by the collection. Here is the snippet from Backbone.js
As you can see the default function just passes data through. You would have to make it work for your response format.
P.S. id recommend placing a breakpoint in Backbone.fetch method to see what format comes from the server and where exactly it breaks the model creation.
而不是
客户期望的
instead of
the client expects