Frage

Is there a good way to roughly achieve the following concept:

var computed = ko.computed(function() {
    readSomeObservables(); //<-- if these change, notify computed
    ko.stopCollectingDependencies();
    readSomeMoreObservables(); //<-- if these change, do not notify computed
    ko.resumeCollectingDependencies();
});

I am aware of peek(), but in this case the computed is invoking methods that were provided from an external module, and the design calls for it to be purely incidental if those methods happen to involve observables.

I have one solution, which is to roughly do this:

window.setTimeout(function() {
    readSomeMoreObservables();
}, 0);

But for obvious reasons, that is hardly ideal and leads to undesired behavior in some situations.

War es hilfreich?

Lösung 2

What about a combination. Create a temp computed for the subscribeables you need to read but do not want to subscribe to. Changing them would update the temp computed but that could be a cheap operation. Your real computed reads the tempComputed with peek accessing the currently cached value.

// this one is updated 
// if any of the subscribeables used in readSomeMoreObservables changes
// but that is hopefully cheap
var tempComputed = ko.computed(function() {
    readSomeMoreObservables();
});


var computed = ko.computed(function() {
    readSomeObservables(); //<-- if these change, notify computed

    // do not update on readSomeMoreObservables
    tempComputed.peek(); 
});

Andere Tipps

For later visitors ...

Version 3.3 has made public ko.ignoreDependencies(callback, callbackTarget, callbackArgs). This is the method used internally by the binding handler processing to avoid creating dependancies from the init function call.

See http://www.knockmeout.net/2015/02/knockout-3-3-released.html

I'm a little late, but this is the solution I use when I am in this situation:

var computed = ko.computed(function() {
    var a = readSomeObservables(); //<-- if these change, notify computed
    var b;

    // capture any dependencies from readSomeMoreObservables
    // in a temporary computed, then immediately dispose
    // of it to release those captured dependencies
    ko.computed(function() { b = readSomeMoreObservables(); }).dispose();

    return a + b; // or whatever
});

This creates a temporary computed in which we call readSomeMoreObservables. The temporary computed sets up a new dependency capture frame and so all the observables read are captured in our temporary computed. We then immediately dispose of the temporary computed to release any dependencies it captured.

Knockout's dependency detection has an ko.dependencyDetection.ignore function. If I understand that correctly you can use that to read the value of subscribeables without creating a dependency to them.

At least the following test runs:

it('Should not subscribe to subscribeables called by ignore', function() {


    var observableInner = ko.observable('initial'),
        observableOuter = ko.observable(),
        called = 0,
        computedInner = ko.computed(function() { return observableInner(); }),
        computedOuter = ko.computed(function() { 
            called += 1;
            // read dependend
            observableOuter();

            // read ignored
            var result = ko.dependencyDetection.ignore(computedInner, null)
            expect(result).toEqual('initial');

            return true;
        });

    expect(called).toEqual(1);

    // update the one we are depending on
    observableOuter(1);
    expect(called).toEqual(2);        

    // update the inner one which should trigger an update to computedInner but not to computedOuter
    observableInner('ignore');
    expect(called).toEqual(2);
});

If you are not interested of new values to those observables, why not just move the reading of the observable outside of the computed scope?

http://jsfiddle.net/uUXWv/2

var observableTwoInitialState = this.observableTwo(); 

this.computed = ko.computed(function() {
    return this.observableOne() + observableTwoInitialState;
}, this);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top