meteor - Template empty initially but renders properly on changing and coming back to route -


i have template named profile contains 3 other templates. 1 of these templates {{> postlist}}

and helper function template is

template.postlist.helpers({ posts: function() { return posts.find({rph: {$in : postsarr}}); } }); 

the problem on going route, postlist template empty, since postsarr calculated later after dom has loaded on basis of other 2 templates. but, if click on other route , come route, template renders properly.

what should template renders itself?

the easiest way session, though it's worst option:

template.postlist.helpers({   posts: function() {     return posts.find({rph: {$in : session.get('postsarr') }});   } }); 

if call session.set('postarr', ...) anywhere in code posts helper update automatically. second option use shared reactive variable:

var postsarr = new reactivevar(); 

and inside helper:

return posts.find({rph: {$in : posts.arr.get() }}); 

now can postsarr.set(...) , should work fine. remember meteor add reactive-var project.

one last doubt is: put reactive variable declaration? in cases can away putting in single "controller" file. work long as: - have 1 instance of template time - code sets ad gets value of reactive variable may put in same file

if 1 of above conditions not hold, option go, btw best possible, put state variable in template's scope. how it:

template.postslist.created = function () {   this.postsarr = new reactivevar(); };  template.postlist.helpers({   posts: function() {     return posts.find({rph: {$in : template.instance().postsarr.get() }});   } }); 

from helpers can access postsarr using template.instance() routine return current template instance, helper called. event handlers, note second argument of handler template instance, you're interested in.

if need access templates, should put state variable on corresponding route controller. assuming you're using iron-router, be:

iron.controller().state.get('postsarr'); 

the iron.controller routine grants access current route controller. read this more details.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -