Knockout.JS-可观察到的更新时屏幕值不刷新

时间:2018-09-07 19:12:45

标签: knockout.js observable

我有一个页面正在通过AJAX发布进行一系列文件上传。以async = true的身份运行帖子会导致(也许是通信)问题,因此我将其转换为async = false。这样可以解决问题,但是随着文件上传,某些屏幕变量不会更新。

我认为这是因为现在帖子不是异步的,处理永远不会离开函数,所以更改后的可观察对象永远不会有机会更新到外部世界(我肯定会对此进行描述)

我已经添加了“ valueHasMutated”调用,但无济于事。

“ uploadBegins”和“ manifestCount”屏幕上的值是有问题的变量。

<!-- ko if: uploadBegins() --> <span data-bind="text:
 manifestCount()"></span> of <span data-bind="text: koXLSdata().length
 - (hasHeaderRow() ? 1 : 0)"></span>&nbsp;records complete 
<!-- /ko -->

uploadBegins应该设置为true,以便该部分可见,并且manifestCount应该在每次上传时进行迭代。

Javascript(已编辑-希望我没有留下重要的内容)             (参考变量用--->指出的行):

    processUploads = function (callback) {
        callbacks.loading();
        getTabDataForModel(viewModel.selectedSupplier());

        var koXLSdata = viewModel.koXLSdata();
        viewModel.badUploads(0);
        viewModel.manifestCount(0);
--->    viewModel.uploadBegins(true);
--->    viewModel.uploadBegins.valueHasMutated();
        viewModel.uploadComplete(false);
        var filetoupload = null;
        var thisWasABadRec = false;
        var theCatCode = 0;

        for (var i = 0; i < viewModel.koXLSdata().length; i = i + 1) {
           uploadFile(koXLSdatarow, filetoupload, theCatCode, viewModel.pmiChecked(), i);
        }
  }

    uploadFile = function (fileData, file, theCatCode, pmiChecked, manifestRow) {
    //("D" contains file upload data, build not included for brevity)
        //vmfile.isUploading(true);
        $.ajax({
            async: false,
            type: "POST",
            url: apiPath + "AccountDocuments",
            data: d,
            cache: false,
            contentType: false,
            processData: false,
            success: function (jqXHR) {
                uploadFileSuccess(jqXHR, vmFile);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                uploadFileError(jqXHR, textStatus, errorThrown, vmFile)
            },
        });
    }

    uploadFileError = function (jqXHR, textStatus, errorThrown, file) {

        viewModel.badUploads(viewModel.badUploads() + 1);
--->    viewModel.manifestCount(viewModel.manifestCount() + 1);
--->    viewModel.manifestCount.valueHasMutated();
        checkForComplete(viewModel.manifestCount());
    },

    uploadFileSuccess = function (jqXHR, file) {

--->    viewModel.manifestCount(viewModel.manifestCount() + 1);
--->    viewModel.manifestCount.valueHasMutated();
        checkForComplete(viewModel.manifestCount());
    },

    checkForComplete = function (manifestCount) {
        var rowsOnly = 0;
        if (viewModel.hasHeaderRow)
            rowsOnly = viewModel.koXLSdata().length - 1;
        else
            rowsOnly = viewModel.koXLSdata().length;

        if (manifestCount === rowsOnly) {
            uploadFileComplete();
        }
    },

同样,上传正常,问题在于屏幕更新。我没有看到该部分打开,也没有看到计数增加。处理完成后,它会以正确的计数弹出,如下所示:

enter image description here

valueHasMutated看起来会强制更改可观察对象,但是我怀疑在循环中屏幕无法更新...

...除非我做...一件事情?

我不知道“东西”是什么。

编辑-“ notifySubscribers()”似乎也没有。

0 个答案:

没有答案