javascript Backbone collection fetch (add:true) 不更新集合

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

Backbone collection fetch (add:true) does not update the collection

javascriptbackbone.jsunderscore.js

提问by TIMEX

loadMore: function(){
    var $this = this;
    console.log(this.Messages); //SAME AS AFTER
    this.Messages.url = '/js/messages/?start=' + this.Messages.length
    this.Messages.fetch({'add':true,
        success:function(){
            console.log($this.Messages); //SAME AS BEFORE??
        },
        error:function(){
        }
    });
 },

The collection is not updated. After this function, the events are fired, and the new items are drawn on the screen. The problem is that the collection did not add the new models.

集合未更新。在此函数之后,将触发事件,并在屏幕上绘制新项目。问题是该集合没有添加新模型。

回答by Radu

As was mentioned in a previous answer the addoption was removed in 1.0.0. You can accomplish the same thing by passing remove: falseinstead. From the docs:

正如之前的回答中提到的,该add选项已在 1.0.0 中删除。你可以通过传递remove: false来完成同样的事情。从文档:

The behavior of fetch can be customized by using the available set options. For example, to fetch a collection, getting an "add" event for every new model, and a "change" event for every changed existing model, without removing anything: collection.fetch({remove: false})

可以使用可用的 set 选项自定义 fetch 的行为。例如,要获取一个集合,为每个新模型获取一个“添加”事件,并为每个已更改的现有模型获取一个“更改”事件,而不删除任何内容:collection.fetch({remove: false})

回答by nackjicholson

Backbone.Collection.fetch():

Backbone.Collection.fetch():

fetch: function(options) {
  options = options ? _.clone(options) : {};
  if (options.parse === void 0) options.parse = true;
  var success = options.success;
  options.success = function(collection, resp, options) {
    var method = options.update ? 'update' : 'reset';
    collection[method](resp, options);
    if (success) success(collection, resp, options);
  };
  return this.sync('read', this, options);
},

So what's up here is, your passed in function is assigned to var succees.
collection[method](resp, options);Is called and in your case method is 'reset'.
collection.resethas to go through and add all your models, fire all the events on the way. I don't know exactly what's going on but it goes through collection.reset, collection.add, model.add, etc...I didn't follow it all.

那么这里的情况是,您传入的函数被分配给var succees.
collection[method](resp, options);被调用,在您的情况下,方法是'reset'.
collection.reset必须经过并添加所有模型,在途中触发所有事件。我不知道到底发生了什么事情,但它通过collection.resetcollection.addmodel.add,等...我没有跟这一切。

I'm not sure what the problem is exactly, I'm sorry about that. I hope I can at least help you try some things so maybe we can figure it out. The line if (success) success(collection, resp, options)is the call to your succes function. What you might try doing is having your success callback accept the passed back arguments and do some consoling of those out:

我不确定到底是什么问题,对此我很抱歉。我希望我至少可以帮助您尝试一些事情,以便我们可以弄清楚。该行if (success) success(collection, resp, options)是对您的成功函数的调用。您可能会尝试做的是让您的成功回调接受传回的参数并对这些参数进行一些安慰:

success: function(collection, resp, options) {
  console.log(collection); // this might do the trick.

  // if not, you could try the following
  collection.on("reset", function(c, options) {
    console.log(c); // see what that gives ya.
  });
}

Another thing is, I couldn't find anywhere in the source or the docs where collection.fetch takes an add option. If I missed it, please let me know I'd like to look it over.

另一件事是,我在源代码或文档中找不到 collection.fetch 带有添加选项的任何地方。如果我错过了,请告诉我我想看一遍。

Good luck, let me know what you find. It might be worth trailing through with a step through debugger too.

祝你好运,让我知道你发现了什么。也可能值得通过调试器逐步完成。

Shit, it strikes me also that console has often showed me the most up to date version of collection objects when it shouldn't have.

妈的,我也觉得控制台经常在不应该有的时候向我展示集合对象的最新版本。

try consoling out the lenghts of the collections instead or something:

尝试安慰一下集合的长度或其他东西:

var len = $this.Messages.length;
console.log(len);


//...
// or in the success callback
var len = collection.length;
console.log(len);

回答by Yinxu

Backbone 1.0 removes this feature, breaking code that depends on this:

Backbone 1.0 删除了这个特性,破坏了依赖于此的代码:

http://backbonejs.org/#Collection-fetch

http://backbonejs.org/#Collection-fetch

Compare with:

与之比较:

"If you'd like to add the incoming models to the current collection, instead of replacing the collection's contents, pass {add: true} as an option to fetch."

“如果您想将传入模型添加到当前集合中,而不是替换集合的内容,请传递 {add: true} 作为获取选项。”

http://htmlpreview.github.com/?https://raw.github.com/documentcloud/backbone/0.9.2/index.html#Collection-fetch

http://htmlpreview.github.com/?https://raw.github.com/documentcloud/backbone/0.9.2/index.html#Collection-fetch

I suggest reverting to an older version of Backbone until this issue is fixed.

我建议恢复到旧版本的 Backbone,直到这个问题得到解决。

回答by freestyle21

in backbone 1.0, you have to trigger reset by hand:

在主干 1.0 中,您必须手动触发重置:

youColloection.fetch({reset: true});