0
votes

So I have this DATA context that I'm gripping and cant figure out how to pass it to the template. Could anyone show or direct me to an example? Do I need a template helper where I will go once more into the DB to retrieve the data, since I already have the data context in the iron router...?

ROUTER.js

Router.route("/employer", {
  name:"employer",
  template:"employer",
  layoutTemplate:'employerLayout',
  // controller:"ProfileController",
    data: function(){
      var currentuser = Meteor.userId();
      Meteor.call("getEmployer", currentuser, function(error, result){
        if(error){
          console.log("error", error);
        }
        if(result){
           return result;
        }
      });

    },

EMPLYER.html

<template name="employer">
  <h1> Employer</h1>

  <h2>{{result}}</h2>

</template>

COLLECTION

meteor:PRIMARY> db.employer.find().pretty()
{
    "_id" : "qCFGZa4ogc5LR56PL", 
    "createdAt" : ISODate("2015-07-18T13:19:16.098Z"),
    "user" : "owfJ4ozrfsp26o8G4" 
}

For example I would like to write out

  <template name="employer">
      <h1> Employer</h1>

      <h2>{{_id}}</h2>
      <h2>{{createdAt}}</h2>
      <h2>{{user}}</h2>


    </template>
1

1 Answers

2
votes

The data hook needs to be synchronous so you can't call a method from within it (and expect a result). However, you could easily move this responsibility to your template with any of the answers to this question. Alternatively, you could try calling a reactive-method from within your data hook - I've never actually done that but it seems like it could work:

data: function() {
  var currentuser = Meteor.userId();
  return ReactiveMethod.call('getEmployer', currentUser);
}

Side note - if it turns out you always call getEmployer only for the current user, then you don't need to pass Meteor.userId(), as this.userId is available to the method.