Update template var


#1

I’m trying to update a reactive var in my template with a value returned from a method on the server. I can see that the server is returning a value but I cant set the var myvar1 in the code below. What am I doing wrong? Thanks for any help.

        Meteor.call('mymethod', arg1, function(err, ret) {
        // Also tried this and self
        Template.instance().myvar1.set(ret);

#2

in what function/helper or so you are calling that?


#3

In Template.mytpl.events

Template.mytpl.created = function() {

this.myvar1 = new ReactiveVar(false);

};

#4

and if you do in that onCreated. (And BTW it is onCreated in current meteor version, not created)

self = this;

and in that callback

self.myvar1.set(ret);

? there was thread regarding similar scoping inside callbacks on these forums.
But I dont remember what was the solution, I would try this one first probably.


#5

Thanks for your answer but I have tried that and also

self.Template.instance().myvar1.set(ret);

with no success.


#6

Maybe this:

var instance = Template.instance();
Meteor.call('mymethod', arg1, function(err, ret) {
  instance.myvar1.set(ret);
});

#7

I did not notice that events handler.

you can pass event AND template instance as argument for event handlers.

so

‘click’: function(event, instance) {
instance.myvar1.set(ret);
}

mby :smiley:


#8

YES, thats the way to do it. Thanks a lot Steve and shock.
In other situations with callbacks it usually works to do self = this and then use self in callback but not this time and with reactive var. Nice and easy solution, thanks.