Search code examples
javascriptmeteormeteor-helper

Calling collection data on helper


I'm trying to build a house rental website and implemented google maps markers for each listing. I have stored the lat,long of each house in the collection along with its title.

But I am not able to call {{olat}} on the helper side as I am able to on the view template to act as placeholder for each. How can I call collection data inside the helper give below.

Template.listing.rendered = function() {
    var tmpl = this;

    VazcoMaps.init({}, function() {

        tmpl.mapEngine = VazcoMaps.gMaps();

        tmpl.newMap2.addMarker({
            lat: 28.6508, //replace this with lat,long variable stored in collection
            lng: 77.3152, //for each listing
            zoom: 11
            icon: '/images/mark.png',
            draggable: false
        });

    });

};

Solution

  • So your olat value is available on your template's data context, and you want to retrieve it in your onRendered hook. Seems a good place to call Template.currentData():

    Template.listing.onRendered(function () {
      var tmpl = this;
      var context = Template.currentData();
    
      VazcoMaps.init({}, function() {
        tmpl.mapEngine = VazcoMaps.gMaps();
    
        tmpl.newMap2.addMarker({
          lat: context.olat,
          lng: context.olong,
          zoom: 11,
          icon: '/images/mark.png',
          draggable: false
        });
      });
    });