Set template stores via validated method

Some data is flowing down from a parent template to a child template:

<template name="mother">
  {{> child my_argument=my_data }}
</template>

I have a validated method that’s called by one of the parent’s helpers:

Template.mother.helpers({
  i_help() {
    my_vmethod.call({userId:Meteor.userId()}, function (err, vmethod_result) {
      if (err) {
        handleError(err.error);
      }
      doSomethingWithResult(vmethod_result);
    });
  },
})

My wish would be to use the return value of the validated method call to set the value of the my_data object. I’m talking about the my_data object of the child template inclusion.

My wish would be to declare something like my_data = my_function(vmethod_result)

Is this possible? If not, what are some alternatives?

I’d probably use a reactiveVar, although I find it a bit odd to be calling a method for data inside a helper?

Template.mother.onCreated({
  // If you are using a check in your child template like {{#if my_argument}}, then
  // you can set the default value as false here, so the child knows not to render
  // whatever's inside unless the helper has assigned a value to the var
  this.myVar = new ReactiveVar(false);
});

Template.mother.helpers({
  i_help () {
    my_vmethod.call({userId:Meteor.userId()}, function (err, vmethod_result) {
      if (err) {
        handleError(err.error);
      }
      Template.instance().myVar.set(vmethod_result);
    });
  },
  my_data () {
    return Template.instance().myVar.get();
  },
});

Thanks @vigorwebsolutions, ur suggestions solved my issues :slight_smile:

In the end it seems that for what I’m trying to achieve, it doesn’t make much sense to call the validated method inside the helper.

I’m now calling the validated from an onCreated callback. It looks like this:

Template.mother.onCreated(function bodyOnCreated() {
  var self = this;
  self.myVar = new ReactiveVar(false);
  my_vmethod.call({userId:Meteor.userId()}, function (err, myvmethod_result) {
    if (err) {
      handleError(err.error);
    }
    self.myVar.set(myvmethod_result);
  });
});

Template.mother.helpers({
  my_data () {
    return Template.instance().myVar.get();
  },
});
1 Like