TIM*_*MEX 8 javascript backbone.js underscore.js
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(){
}
});
},
Run Code Online (Sandbox Code Playgroud)
该集合未更新.在此功能之后,将触发事件,并在屏幕上绘制新项目.问题是该集合没有添加新模型.
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);
},
Run Code Online (Sandbox Code Playgroud)
所以这里的情况是,您传入的函数被分配给var succees.
collection[method](resp, options);被调用,在你的情况下方法是'reset'.
collection.reset必须检查并添加所有模型,触发途中的所有事件。我不知道到底发生了什么,但它经历了、、、collection.reset等等……我没有完全遵循。collection.addmodel.add
我不确定问题到底是什么,对此我感到很抱歉。我希望我至少可以帮助你尝试一些事情,这样也许我们可以找到答案。该行if (success) success(collection, resp, options)是对您的 succes 函数的调用。您可能会尝试做的是让成功回调接受传回的参数并对这些参数进行一些安慰:
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.
});
}
Run Code Online (Sandbox Code Playgroud)
另一件事是,我在源代码或文档中找不到 collection.fetch 采用添加选项的任何地方。如果我错过了,请告诉我我想看一下。
祝你好运,让我知道你发现了什么。也可能值得通过调试器进行逐步跟踪。
糟糕,令我震惊的是,控制台经常向我显示不应该显示的最新版本的集合对象。
尝试安慰集合的长度或者其他东西:
var len = $this.Messages.length;
console.log(len);
//...
// or in the success callback
var len = collection.length;
console.log(len);
Run Code Online (Sandbox Code Playgroud)