knockout.js 如何清除从以前对视图的访问中填充的observableArray的内容

wmtdaxz3  于 2022-11-10  发布在  其他
关注(0)|答案(4)|浏览(151)

我有一个单页应用程序,该应用程序使用挖空进行数据绑定。我的单页应用程序中的CAApproval.html视图在视图模型代码中有一个名为AllCertificates的可观察队列。它在页面上填充得很好。当您通过单击页面的navigation.html部分中的链接离开该视图,然后返回CAApproval页面时,上一次访问的值仍在AllCertificates observableArray中,因此会显示在CAApproval视图中。
我需要在每次用户返回到使用该观察窗的CAApproval页时清除AllCertificates观察窗的内容,这样,如果用户离开该页并返回,观察窗的内容将为空,因此屏幕上不会显示任何数据。

define(['services/logger', 'durandal/system', 'durandal/plugins/router', 'services/CertificateDataService','controls/Lucas'],

       function(logger, system, router, CertificateDataService) {
        var allCertificates = ko.observableArray([]);

    var activate = function () {
            // go get local data, if we have it
            return SelectAllCerts(),SelectMyCerts(), GetCertificateDetails(), GetDDABankNums();
            };
        var vm = {
            activate: activate,
            allCertificates: allCertificates,
    SelectAllCerts: SelectAllCerts

        });

    return vm;

    function SelectAllCerts() {
                return CertificateDataService.getallCertificates(allCertificates);
        }
    });

如何在用户每次访问该页面时清除observablearray的内容(不是在页面本身内导航时,只有当用户来自另一个页面时才清除observablearray)?

xmakbtuz

xmakbtuz1#

同样,knockout observableArray也有一些有趣的方法。调用removeAll来清除所有项目。
看看官方网站observable array manual

self.mycertificates = ko.observableArray(['C1', 'C2']);  
self.mycertificates.removeAll();
ohfgkhjo

ohfgkhjo2#

只需在activate函数中将其设置为nothing(allCertificates([]))即可,该函数在每次加载视图模型时都会调用-

function(logger, system, router, CertificateDataService) {
    var allCertificates = ko.observableArray();

var activate = function () {
    allCertificates([]);
    // go get local data, if we have it
    return SelectAllCerts(),SelectMyCerts(), GetCertificateDetails(), GetDDABankNums();
};

var vm = {
    activate: activate,
    allCertificates: allCertificates,
    SelectAllCerts: SelectAllCerts
});
goqiplq2

goqiplq23#

对于杰里米T(评论空间不够)。
第一个原因,对我来说绝对足够的是存在公开可用的API用于所需的目的。
但是要估计性能,您可以检查源代码。“observableArray”也是“可观察的”,并向对象中注入了其他函数。
初始化如下所示:

ko.observableArray = function (initialValues) {
    initialValues = initialValues || [];

    if (typeof initialValues != 'object' || !('length' in initialValues))
        throw new Error("The argument passed when initializing an observable array must be an array, or null, or undefined.");

    var result = ko.observable(initialValues);
    ko.utils.extend(result, ko.observableArray['fn']);
    return result.extend({'trackArrayChanges':true});
};

ko.observable = function (initialValue) {
    var _latestValue = initialValue;

    function observable() {
        if (arguments.length > 0) {
            // Write

            // Ignore writes if the value hasn't changed
            if (!observable['equalityComparer'] || !observable['equalityComparer'](_latestValue, arguments[0])) {
                observable.valueWillMutate();
                _latestValue = arguments[0];
                if (DEBUG) observable._latestValue = _latestValue;
                observable.valueHasMutated();
            }
            return this; // Permits chained assignments
        }
        else {
            // Read
            ko.dependencyDetection.registerDependency(observable); // The caller only needs to be notified of changes if they did a "read" operation
            return _latestValue;
        }
    }
    if (DEBUG) observable._latestValue = _latestValue;
    ko.subscribable.call(observable);
    observable.peek = function() { return _latestValue };
    observable.valueHasMutated = function () { observable["notifySubscribers"](_latestValue); }
    observable.valueWillMutate = function () { observable["notifySubscribers"](_latestValue, "beforeChange"); }
    ko.utils.extend(observable, ko.observable['fn']);

    ko.exportProperty(observable, 'peek', observable.peek);
    ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);
    ko.exportProperty(observable, "valueWillMutate", observable.valueWillMutate);

    return observable;
}

并删除所有元素,如下所示:

'removeAll': function (arrayOfValues) {
        // If you passed zero args, we remove everything
        if (arrayOfValues === undefined) {
            var underlyingArray = this.peek();
            var allValues = underlyingArray.slice(0);
            this.valueWillMutate();
            underlyingArray.splice(0, underlyingArray.length);
            this.valueHasMutated();
            return allValues;
        }
        // If you passed an arg, we interpret it as an array of entries to remove
        if (!arrayOfValues)
            return [];
        return this['remove'](function (value) {
            return ko.utils.arrayIndexOf(arrayOfValues, value) >= 0;
        });
    }
rxztt3cl

rxztt3cl4#

初始化数组变量

self.allCertificates = ko.observableArray([])

删除数组的所有值

self.allCertificates.removeAll()

相关问题