How to call computation.stop () from Depsautorun ()?

I am trying to stop Deps.autorun () from executing again after the condition is true:

Deps.autorun(function(computation){

    var first_product = Collection.products.findOne({enabled:true});

    if( first_product ){
        Session.setDefault( 'purchase_dialog.selected_product_id', first_product._id );
        console.log( 'set here');
        computation.stop();
    }

});

I see the β€œmany here” many times, and it continues to run. Why is .stop () not working?

+3
source share
2 answers

If Deps.autoruncalled inside another reactive environment (say, c Template.foo.rendered), then it will continue to be executed every time the parent calculation is performed, and stopping the child’s calculation will not stop the parental calculation.

To fix . Move the call to Deps.autorunthe outside of the reactive area, to the upper level.

+2
source

Call this.stop()

Deps.autorun(function(){
    if ( somethingHappens ){
        this.stop();
    }
});

or

Deps.autorun(function(computation){
    if ( somethingHappens ){
        computation.stop();
    }
});
+1
source

All Articles