Cereal Killer
Cereal Killer

Reputation: 3414

Can't get reactive var from helper in meteor js

I'm trying to write the value of a reactive var to a meteor template:

Template.form.rendered = function() {
    this.color = new ReactiveVar();
    this.color.set('#333555');
};

and then I've defined a "currentColor" helper to use in the template to print the color: {{currentColor}}

Template.form.helpers({
    currentColor: fuction() {
        return Template.instance().color.get();
    }
});

but it doesn't work; so I've tried to add a couple of console.log:

Template.form.helpers({
    currentColor: fuction() {
        console.log(Template.instance());
        console.log(Template.instance().color);
    }
});

The strange thing is that the first console log shows a Blaze.TemplateInstance with the color property:

Blaze.TemplateInstance {...}
    color: ReactiveVar
        curValue: "#333555"
        dep: Tracker.Dependency
        equalsFunc: undefined
    ...

but the second log is "undefined"; Can someone help me to understand this?

Upvotes: 1

Views: 3894

Answers (1)

Jeremy S.
Jeremy S.

Reputation: 4639

You should place the ReactiveVar definition in your template.created() function, not the template.rendered() function.

Upvotes: 6

Related Questions