我使用Fine Uploader将文件直接上传到Amazon S3(与问题无关)。
我正在做的是生成一个"附件" backbone.js模型(生成唯一ID),并通过setParams
事件中的submit
将该ID传递给上传者,以便我在complete
事件中识别附件并采取适当的行动。
在同时上传多个文件时(使用10个文件尝试)会出现问题。 Fine Uploader显然调用了submit
回调太快,我在complete
事件中获得了重复的模型ID。
如果我在submit
事件中添加断点并且什么也不做,只需点击"播放"再次,并发问题不再存在。
我的部分代码是:
var self = this;
this.uploader.on('submit', function() {
var attachment = new Market.InboxPage.Models.Attachment(); // generates a new model with a unique ID
self.attachments[attachment.cid] = attachment; // self.attachments is a model collection
self.uploader.setParams({'cid': attachment.cid});
});
this.uploader.on('complete', function(event, id, file, response) {
self.attachments[response.metadata.cid].set({ // getting duplicate data here
filename: response.metadata.qqfilename,
key: response.metadata.key,
filesize: response.metadata.filesize
});
self.attachments[response.metadata.cid].updateInDom();
return false;
});
答案 0 :(得分:1)
问题在于您拨打setParams
。每次调用setParams
时,您都会有效地覆盖所有现有文件的参数。相反,您应该在每次调用setParams
时定位特定文件。 setParams
允许您通过将文件ID指定为setParams
调用的参数来执行此操作。例如:
this.uploader.on('submitted', function(event, id, name) {
var attachment = new Market.InboxPage.Models.Attachment(); // generates a new model with a unique ID
self.attachments[attachment.cid] = attachment; // self.attachments is a model collection
self.uploader.setParams({'cid': attachment.cid}, id); });
注意我如何将您的逻辑切换到submitted
事件,该事件在文件成功提交后调用,并且ID可用。