Almaju
Almaju

Reputation: 1383

Meteor ReactiveVar behaviour?

I use ReactiveVar to extend the subscription when the user reach the end of the page but I experienced some strange behaviours lately (might be because some package updates?)

Template.hello.onCreated(function(){
    var tpl = this;

    tpl.dict = new ReactiveDict();
    tpl.var = new ReactiveVar();
    Session.set('session', undefined);

    Tracker.autorun(function(){
        console.log(tpl.dict.get('foo'), tpl.var.get(), Session.get('session'));
    })
});

Template.hello.onRendered(function(){
    var tpl = this;

    console.log(this.dict.get('foo'), this.var.get(), Session.get('session'));

    tpl.dict.set('foo', 'foo');
    tpl.var.set('foo');
    Session.set('session', 'foo');
});

This gives:

undefined undefined undefined (onCreated)
undefined undefined undefined (onRendered)
foo foo foo (onCreated, after autorun)

My basic question is: is this supposed to be the normal behaviour? The ReactiveVar/Dict aren't supposed to autorun automatically when changed?

Any idea on this or am I just dumb? :)

Upvotes: 0

Views: 62

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21364

This is perfectly expected behavior. onCreated fires before onRendered and only at the very end of onRendered are you setting any values to your variables. And yes, only code that is in an autorun block (implicitly or explicitly) will rerun when dependencies change. onRendered and onCreated themselves are not reactive (unlike, e.g., template helpers).

Upvotes: 1

Related Questions