5

次の概念を大まかに達成するための良い方法はありますか?

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

私は知っていますpeek()が、この場合、計算は外部モジュールから提供されたメソッドを呼び出します。設計では、これらのメソッドにオブザーバブルが含まれる場合、純粋に偶発的なものである必要があります。

私には1つの解決策があります。それは、大まかにこれを行うことです。

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

しかし、明らかな理由から、それはほとんど理想的ではなく、状況によっては望ましくない動作につながります。

4

5 に答える 5

6

後の訪問者のために...

バージョン3.3では、ko.ignoreDependencies(callback、callbackTarget、callbackArgs)が公開されています。これは、init関数呼び出しからの依存関係の作成を回避するために、バインディングハンドラー処理によって内部的に使用されるメソッドです。

http://www.knockmeout.net/2015/02/knockout-3-3-released.htmlを参照してください

于 2015-08-04T03:25:39.257 に答える
4

組み合わせはどうですか。読む必要があるが購読したくない購読可能ファイルに対して計算された一時を作成します。それらを変更すると、計算された温度が更新されますが、これは安価な操作になる可能性があります。実際の計算では、現在キャッシュされている値にアクセスするピークでtempComputedを読み取ります。

// 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(); 
});
于 2013-01-30T14:30:53.510 に答える
2

私は少し遅れていますが、これは私がこの状況にあるときに使用する解決策です:

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

これにより、を呼び出す一時的な計算が作成されますreadSomeMoreObservables。一時計算では、新しい依存関係キャプチャフレームが設定されるため、読み取られたすべてのオブザーバブルが一時計算でキャプチャされます。次に、一時的な計算をすぐに破棄して、キャプチャした依存関係を解放します。

于 2013-03-13T04:36:53.083 に答える
1

Knockoutの依存関係検出にはko.dependencyDetection.ignore機能があります。私がそれを正しく理解していれば、それを使用して、サブスクライブ可能なものへの依存関係を作成せずに、サブスクライブ可能なものの値を読み取ることができます。

少なくとも次のテストが実行されます。

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);
});
于 2013-01-30T13:31:39.277 に答える
0

これらのオブザーバブルの新しい値に関心がない場合は、オブザーバブルの読み取り値を計算されたスコープの外に移動してみませんか?

http://jsfiddle.net/uUXWv/2

var observableTwoInitialState = this.observableTwo(); 

this.computed = ko.computed(function() {
    return this.observableOne() + observableTwoInitialState;
}, this);
于 2013-01-30T13:08:11.507 に答える