Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent computed from collecting dependencies

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.

like image 401
Rex M Avatar asked Jan 30 '13 12:01

Rex M


2 Answers

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

like image 94
RockResolve Avatar answered Sep 25 '22 13:09

RockResolve


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(); 
});
like image 37
delixfe Avatar answered Sep 21 '22 13:09

delixfe