How to Clear Contents of an observableArray That was Populated from Previous Visits to a View

前端 未结 4 1134
猫巷女王i
猫巷女王i 2021-02-06 20:59

I have a Single Page Application that uses knockout for the data binding. The CAApproval.html view in my single page application has an observeablearray named AllCertificates i

4条回答
  •  生来不讨喜
    2021-02-06 21:55

    For Jeremy T (not enough space in comment).
    First reason and absolutely enough for me is existence of publicly available API for desired purpose.

    But to estimate performance you can check source. "observableArray" is also "observable" with additional functions injected into object.

    So initialization looks like this:

    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;
    }
    

    And remove all elements looks like this:

    '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;
            });
        }
    

提交回复
热议问题