Backbone:从服务器获取集合

发布于 2024-10-28 03:51:50 字数 1099 浏览 1 评论 0原文

我正在尝试从我的服务器获取集合。 我使用的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不即不离 2024-11-04 03:51:50

由于您已经确定您的响应格式不是 Backbone 所期望的,因此您应该重写 YourModel.parse 函数,该函数应该从服务器获取响应并返回集合可接受的模型数组。这是来自 Backbone.js 的片段

// **parse** converts a response into a list of models to be added to the
// collection. The default implementation is just to pass it through.
parse : function(resp) {
  return resp;
},

,您可以看到默认函数只是传递数据。您必须使其适合您的响应格式。

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

// **parse** converts a response into a list of models to be added to the
// collection. The default implementation is just to pass it through.
parse : function(resp) {
  return resp;
},

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.

_蜘蛛 2024-11-04 03:51:50

而不是

["id1", "id2", ..., "idn"]

客户期望的

[{"id": "id1"}, ... {"id": "idn"}]

instead of

["id1", "id2", ..., "idn"]

the client expects

[{"id": "id1"}, ... {"id": "idn"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文