5

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.

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • Why do you read them from the computed if you do not want it to update when those change? – Anders Jan 30 '13 at 12:41
  • I'll restate what was said in the question: the situation is fairly complex in that external modules can provide additional callbacks that need to be *invoked* at a specific point in the execution flow, but if those callbacks happen to involve reading from observables, those observables should not participate in the computed. So I could redesign the entire module (expensive) or find a workaround (cheap). – Rex M Jan 30 '13 at 12:45
  • So the computed does not need to publish to its subscribers when those change? Correct? – Anders Jan 30 '13 at 12:52
  • @Anders close - the computed should not subscribe to those observables in the first place; because it must not re-evaluate its own read function, much less notify its subscribers. – Rex M Jan 30 '13 at 12:59
  • You cant get teh team behind the readSomeMoreObservables to extend it to either use peek or standard read? – Anders Jan 30 '13 at 14:07
  • @Anders that would force other modules to make decisions about whether to read their own observables using peek or the normal way based on the internal implementation of this module. Definitely want to avoid that. – Rex M Jan 30 '13 at 19:46
  • @Anders also, I have a great friend in Göteborg named Anders who's also a developer and when I first saw how quickly an Anders responded I thought it was him :) – Rex M Jan 30 '13 at 19:47

5 Answers5

6

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

RockResolve
  • 1,423
  • 21
  • 29
  • What's the advantage over `.peek()` function? – Joel May 09 '16 at 10:14
  • OP discounted peek(). I think because peek() only ignores the initial observable, whereas ignoreDependencies creates a new scope surrounding the callback. – RockResolve May 09 '16 at 11:18
4

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(); 
});
delixfe
  • 2,471
  • 1
  • 21
  • 35
  • Good idea, I tested that with teh fiddle from my answer, http://jsfiddle.net/uUXWv/3/ – Anders Jan 30 '13 at 16:37
  • Oops, posted a comment before I fully saw the implications of this solution. This is a very good idea - I added `deferEvaluation` to ensure invocation order stays the same: http://jsbin.com/etuxuq/1/ Thanks! – Rex M Jan 30 '13 at 19:49
  • This is good idea, but for later visitors, @RockResolve has provided a more modern solution [below](http://stackoverflow.com/a/31800146/348336). – Gildor Aug 04 '15 at 22:41
2

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.

Brandon
  • 38,310
  • 8
  • 82
  • 87
1

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);
});
delixfe
  • 2,471
  • 1
  • 21
  • 35
  • ko.dependencyDetection is a internal helper function – Anders Jan 30 '13 at 13:43
  • Oh, I missed that one. You could create your "own build" of knockout exporting that property: ko.exportSymbol('dependencyDetection', ko.dependencyDetection); – delixfe Jan 30 '13 at 13:50
0

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);
Anders
  • 17,306
  • 10
  • 76
  • 144
  • My fiddle had a bug with the update of observable two try the latest one – Anders Jan 30 '13 at 13:10
  • This does not address the question, as I do not control all of the code in the real scenario. – Rex M Jan 30 '13 at 13:28
  • ko does not support this out of the box, you do need to use peek and since you cant change the function readSomeMoreObservables that going to be hard. – Anders Jan 30 '13 at 13:35