0

I'm new in meteor.js and i'm practising building a simple and little app. My question is how to use the template data get from mongodb in templates inside. This is the structure:

router.js

Router.route('/point/:_id', {name: 'pointDetail', data: function() {
    return InterPoints.findOne(this.params._id);
} });

template pointDetail.html

<template name="pointDetail">
 <some tags>
   {{datathree}}
 </some tags>
   {{> map}}
</template>

template map.html

<template name="map">
  <div class="map-container">
    {{> googleMap name="exampleMap" options=exampleMapOptions}}
  </div>
</template>

map.js

  Template.map.helpers({
    exampleMapOptions: function() {
      // Make sure the maps API has loaded
      if (GoogleMaps.loaded()) {
        // Map initialization options
        return {
          center: new google.maps.LatLng(coordX, coordY),
          zoom: 8
        };
      }
    }
  });

I want to fill coorsx and coordy in map.js using the data I'm retrieving from the router. Someting like coordx = {{coordsx}} It is possible?

Thanks a lot.

proquibas
  • 91
  • 3
  • 16
  • I think you could simply apply [collections](https://www.meteor.com/tutorials/blaze/collections) there. If not, take a look at [session variables](http://meteortips.com/first-meteor-tutorial/sessions/). – rishat Aug 12 '15 at 08:57

1 Answers1

0

You can retrieve a parent template's data context through Blaze.currentView.parentView.templateInstance(). Assuming your Interpoints document has properties coordx and coordy:

Template.map.helpers({
  exampleMapOptions: function() {
    // Make sure the maps API has loaded
    if (GoogleMaps.loaded()) {
      // Map initialization options
      var parentTemplate = Blaze.currentView.parentView.templateInstance();
      var coordX = parentTemplate.data.coordx;
      var coordY = parentTemplate.data.coordy;
      return {
        center: new google.maps.LatLng(coordX, coordY),
        zoom: 8
      };
    }
  }
});
SylvainB
  • 4,765
  • 2
  • 26
  • 39
  • Thanks BraveKenny, I try it but is not working. my Interpoints document have properties like language,name, type,district,coordx,coordy. If i use your code, it throw an error: **Exception in template helper: TypeError: Cannot read property 'coordx' of undefined** . I try it in diferent levels but still the same. I put the code in detailPoints.js with a console.log but same error. Thanks! – proquibas Aug 12 '15 at 09:19
  • Hm, I think I was wrong, [there is a difference between nested data contexts and nested templates](http://stackoverflow.com/questions/27949407/how-to-get-the-parent-template-instance-of-the-current-template). I edited my answer with what I could gather from the linked question. – SylvainB Aug 12 '15 at 09:29
  • Thanks BraveKenny, now it works perfectly! Rishat Muhametshin solution's works fine too. Thanks a lot for your help, regards! – proquibas Aug 12 '15 at 10:18