Loop call within template

Hi there!

I have a helper that is being picked up within a template loop. What it does is it gets a specific detail from the server (db)

videoCreator: -> Meteor.call 'getVideoCreator', id, (error, result) -> Session.set 'video', result Session.get (result)
(can’t figure out how to indent) But basically what it does is it tries to get the result from the database. However, this is being run on a template loop. And looks messy – it’s acting weird.

Any idea how to run a Meteor.call from client within the template loop?

Three ` before and after code

Didn’t you just say this was messy? :wink: How about running it in the onCreated function?

Template.myTemplate.onCreated(function () {
    // Just guessing
    var id = Blaze.getData().id;
    Meteor.call( 'getVideoCreator', id, (error, result) => {
         Session.set( 'videoCreator', result);

     videoCreator() {
        return Session.get("videoCreator")

Now you will only get it once.

It’s not a good idea to get and set the same reactive variable inside a helper (or any reactive context).

Better is to make the call and the set in the onCreated callback and reserve the get for the helper.

Thanks guys!

But remember, I’m also having this:

{{#each video}}
<p>Name: {{videoCreator}}</p>

How can the Meteor.call be triggered while on the loop?

PS. I saw in one StackOverflow answer about the use of reactiveVar. Is that needed here?

Ah, so it is in a list. Then you would do something similar.

Template.videoItem.onCreated(function () {
    var self = this;
    self.creator = new ReactiveVar("");

    var id = Blaze.getData().id;
    Meteor.call( 'getVideoCreator', id, (error, result) => {

     videoCreator() {
        return Template.instance().creator.get()

And then do this

{{#each video}}
   {{> videoItem}}
<template name="videoItem">
   <p>Name: {{videoCreator}}</p>

Makes sense? Usually a good idea to use an instance specific RactiveVar rather than Session anyway.

1 Like

I’m getting a ReactiveVar is not defined error

My code:

Template.video_item.onCreated ->
	self = this
	self.creator = new ReactiveVar('')
	id = Blaze.getData()._id

	Meteor.call 'getVideoCreator', id, (error, result) ->
		self.creator.set result

	videoCreator:  ->

have you added the reactive-var package?

meteor add reactive-var

Opps… I didn’t know it’s a package. Thanks. Will try that.

easily missed


Thanks guys. It worked! This thing has been bothering me for long. You’ve all been very helpful.