danSiebes
danSiebes

Reputation: 384

Why is my template helper that uses session.get not reactive?

In my client code I have a Session which is set to an object in a global function:

somefunctions.js

updateTimelineItem = function(newSelection){
    var selectedItem = $.grep(Session.get('liveProjectData').items,function(e){return e.position ==newSelection.parent().index()});
    Session.set('selectedItem',selectedItem[0]);
};

However, in a template file where I need to display portions of this session's object data, my helper does not fire after the session is set.

mytemplate.js

Template.mytemplate.helpers({    
    selectedItem: function(){
        console.log('reactive update. new item selected.');
        return Session.get('selectedItem');
    }
})

Example of what the session stores

Object { position: 0, type: "image", source: "imgur", source-url: "https://dl.dropboxusercontent.com/u…", provider: "magic", animation: "puff", thumb: "https://dl.dropboxusercontent.com/u…", fullsize: "https://dl.dropboxusercontent.com/u…", duration: 1000 }

I have tried to find documentation regarding when a Session would not be reactive without much luck. I know the session is set because I can write Session.get('selectedItem') in a browser console and I get the expected output.

Thank you for any help.

Upvotes: 0

Views: 247

Answers (1)

danSiebes
danSiebes

Reputation: 384

The reason my reactive code was not running is because I did not access the specific reactive variable in the spacebars of that template. Although Template.helpers is a reactive computation, it executes based on usage of reactive vars in the dom. This is unlike other reactive computations.

Information regarding this is hard to come by but it is hinted at in the Meteor documentation for Template.currentData() and its usage inside of helper.

The work I needed to do with the reactive variable was outside of the view and should therefore not be used in Template.helper but instead template.autorun.

Upvotes: 0

Related Questions