Javascript BackboneJS collection.reset() 与 collection.fetch()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13519515/
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-24 12:16:30  来源:igfitidea点击:

BackboneJS collection.reset() vs collection.fetch()

javascriptbackbone.js

提问by benhowdle89

I have read and read the docs on these two methods, but for the life of me cannot work out why you might use one over the other?

我已经阅读并阅读了关于这两种方法的文档,但是我一生都无法弄清楚为什么您可以使用一种方法而不是另一种方法?

Could someone just give me a basic code situation where one would be application and the other wouldn't.

有人可以给我一个基本的代码情况,其中一个是应用程序,另一个不是。

回答by McGarnagle

resetsets the collection with an array of models that you specify:

reset使用您指定的模型数组设置集合:

collection.reset( [ { name: "model1" }, { name: "model2" } ] );

fetchretrieves the collection data from the server, using the URL you've specified for the collection.

fetch使用您为集合指定的 URL 从服务器检索集合数据。

collection.fetch( { url: someUrl, success: function(collection) {
    // collection has values from someUrl
} } );


Here's a Fiddle illustrating the difference.

这是一个说明差异的小提琴。

回答by Cyclone

We're assuming here that you've read the documentation, else it'l be a little confusing here.

我们在这里假设您已经阅读了文档,否则这里会有点混乱。

If you look at documentation of fetchand reset, what it says is, suppose you have specified the urlproperty of the collection- which might be pointing to some server code, and should return a jsonarray of models, and you want the collectionto be filled with the modelsbeing returned, you will use fetch.

如果你看的文件读取复位,它说的是,假设你指定的url财产collection-这可能会指向一些服务器代码,并应返回json的阵列models,并且希望collection将充满models幸福返回,您将使用fetch.

For example you have the following json being returned from the server on the collection url:

例如,您在集合 url 上从服务器返回以下 json:

[{
  id : 1, 
  name : "a"
 }, {
  id : 2, 
  name : "b"
 }, {
  id : 3, 
  name : "c"
 }]

Which will create 3 models in your collection after successful fetch. If you hunt for the code of collection fetchhereyou will see that fetch will get the response and internally will call either resetor addbased on options specified.

成功获取后,这将在您的集合中创建 3 个模型。如果您查找collection fetch此处的代码,您将看到 fetch 将获得响应,并且内部将根据指定的选项调用reset或调用add

So, coming back to discussion, resetassumes that we already have json of models, which we want to be stored in collection, we will pass it as a parameter to it. In your life, ever if you want to update the collection and you already have the models on client side, then you don't need to use fetch, resetwill do your job.

因此,回到讨论中,reset假设我们已经拥有json of models,我们希望将其存储在集合中,我们将其作为参数传递给它。在您的生活中,如果您想更新集合并且您已经在客户端拥有模型,那么您不需要使用fetchreset将完成您的工作。

Hence, if you want to the same json to be filled in the collection with the help of reset you can do something like this:

因此,如果您想在重置的帮助下将相同的 json 填充到集合中,您可以执行以下操作:

var _self = this;
$.getJSON("url", function(response) { 
  _self.reset(response); // assuming response returns the same json as above
});

Well, this is not a practice to be followed, for this scenario fetchis better, its just used for example.

嗯,这不是一个可以遵循的做法,因为这个场景fetch更好,它只是用于示例。

Another example of reset is on the documentationpage.

另一个重置示例在文档页面上。

Hope it gives a little bit of idea and makes your life better :)

希望它能给你一点想法,让你的生活更美好:)

回答by funnydaredevil

reset() is used for replacing collection with new array. For example:

reset() 用于用新数组替换集合。例如:

@collection.reset(@full_collection.models)

would load @full_collections models, however

但是会加载@full_collections 模型

@collection.reset()

would return empty collection. And fetch() function returns default collection of model

将返回空集合。和 fetch() 函数返回模型的默认集合