Javascript Backbone:从服务器获取集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5501562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:29:19  来源:igfitidea点击:

Backbone: fetch collection from server

javascriptjqueryrestbackbone.js

提问by Vlad Gurovich

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:

我正在尝试从我的服务器中获取一个集合。我使用的是 0.3.3 版(不是来自 github 的大师)但是我在这个异常中运行:

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

/api/collection 中的元素以 JSON 格式解析。我试图以各种格式返回它们

["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.

[编辑]通过coll.fetch({error: errorFunc});异常保持不变来设置错误无济于事。

[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.

[Edit2]好吧,在使用响应对象collection.fetch()调用之前,似乎一切正常collection.refresh()。我没有覆盖任何这些功能。

[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.

[Edit3]错误出在collection.add()方法中,原因是我的元素是一个字符串列表......我的服务器错误地发送了它们。

回答by Vlad Gurovich

Since you already identified that your response format is not what Backbone expect, you should override YourModel.parsefunction 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

由于您已经确定您的响应格式不是 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;
},

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.

PS id 建议在 Backbone.fetch 方法中放置一个断点,以查看来自服务器的格式以及它在何处中断模型创建。

回答by Vlad Gurovich

instead of

代替

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

the client expects

客户期望

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