gpt4 book ai didi

javascript - 通知 ViewModel 模型完成?

转载 作者:行者123 更新时间:2023-12-02 15:08:56 25 4
gpt4 key购买 nike

我在 javascript/knockout 中有一个文件上传实用程序。在我的 View 模型中,我在模型 .upload() 上调用异步 xhr 类型请求的函数。我想在 viewModel 中知道的是所有 xhr 请求何时完成:

this.upload = function () {
var self = this;
for (var i = 0; i < self.files().length; i++) {
self.files()[i].upload();
}

this.uploadComplete(true);
};

this.uploadComplete(true); 当然不起作用,因为调用是异步的。从模型返回到 View 模型以表明上传已完成的正确方法是什么(注意可以有多个文件)。

最佳答案

文件的 upload 方法应该返回 Promise (或类似的东西)。您可以将它们收集到一个数组中,然后

Promise.all(arrayOfPromises).then(function () {
self.uploadComplete(true);
});

关于javascript - 通知 ViewModel 模型完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34909130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com