How can I modify a ReactiveVar inside of a jQuery plugin event?


#1

I have a ReactiveVar that I need to read from and modify inside of an initialized jQuery plugin. Unfortunately I can’t find a way to call my template’s context inside of the jQuery event callbacks. This is all client side. I can’t use a Session here. Here is some code:

Setup my var

Template.player.onCreated(function(){
   this.isPlaying = new ReactiveVar;
   this.isPlaying.set('false');
})

Init my jQuery plugin

Template.player.onRendered(function() {
    initPlayer()
});

relevant portion of initPlayer():

master = new TimelineMax({
    paused: true
});
master.eventCallback("onUpdate", function() {
        //Template.instance().isPlaying.set(false) //can't do this here
        //reactive variables in jquery events are out of scope?
    }
});

I have tried running the init function in an autotracker without any luck. I know I could use a Session here but I really want a reactive variable that will be destroyed along with the template. Any ideas of how to model this?


#2

Pass the current instance of your template to your init function :

Template.player.onRendered(function() {
initPlayer( this )
});

var initPlayer = function( template ){
template.isPlaying.set( value )
}


#3

Thank you that does it!