Skip to content Skip to sidebar Skip to footer

Meteor : Autorun Won't Make My Code Dynamic

I'm working hard on a meteor App which goal is to dynamically display on a google map the path of a vehicle, for example a boat on the sea. Now, I see this library called gmaps.j

Solution 1:

Try using nested templates for this. So that your wrapper template subscribes to data and only renders nested template when subscriptions is ready:

//wrapper template that manages subscription
Template.wrapper.onCreated(function() {
  this.subscribe('allRuns');
});

Template.runs_show.onRendered(function() {
  google_maps.KEY = "MY API KEY";
  google_maps.load(function(google){
    var map = new gmaps({
      el: '#map-gmaps',
      lat: -12.043333,
      lng: -77.028333
    });

    Tracker.autorun(function() {
      // Run.find will re-trigger this whole autorun block// if any of the elements of the collection gets changed or// element gets created or deleted from collection// thus making this block reactive to data changes// this will also work with findOne in case you only want to// one run onlyvar runs = Run.find({"maxSpeed" : "75"}).fetch();
      // map.clear() or something like that to remove all polylines// before re-rendering them
      runs.forEach(function(run){
        map.drawPolyline({
          path          : path,
          strokeColor   : '#FC291C',
          strokeOpacity : 0.85,
          strokeWeight  : 6
        });
      });

    });
    // ^^ this is pretty dumb and re-renders all items every time// something more intelligent will only re-render actually // changed items and don't touch those who didn't change but// that will require a slightly more complex logic

  });
});

wrapper.html:

<template name="wrapper">
  {{#if Template.subscriptionsReady }}
    {{> runs_show }}
  {{/if}}
</template>

P.S. this is mostly pseudo code since I never tested it, so just use it as a guide

Solution 2:

Seems the issue is that the subscribe callback is not a reactive context. Try doing what worked for others here and here, as well as putting the tracking in your onRendered.

 Template.templateName.onRendered(function() {
     varself = this;
     self.autorun(function() {
         self.subscribe('allRuns',function() {
             Tracker.autorun(function(){
                 ...
             }
         })
     })
 })

Post a Comment for "Meteor : Autorun Won't Make My Code Dynamic"