javascriptmeteormeteor-tracker

meteor where to put global tracker.autorun


I am wondering where to put a Tracker.autorun in oder to guarantee that everything else is loaded before. I thought that

Meteor.startup(function(){...});

is used for such situations, but when I reference the Geolocation object from mdg:geolocation it tells me that it is not defined yet:

Uncaught TypeError: Cannot read property 'lng' of null

I use the following workaround, but I hope that there is a more elegant solution:

 Meteor.startup(function () {
    trackerGeolocationInit = setInterval(enableLocationTracking, 100);
});

enableLocationTracking = function(){

    var location = Geolocation.latLng();
    if(location === null)
        return;
    else
        clearInterval(trackerGeolocationInit);

    Tracker.autorun(function () {
        var location = Geolocation.latLng();
        Meteor.users.update(Meteor.userId(), {
            $set: {
                "profile.location": {
                    type: 'Point',
                    coordinates: [location.lng, location.lat]
                }
            }
        });
    });
}

Solution

  • I'm writing an app for fun and this works:

       Tracker.autorun(function () {
          if(Meteor.userId())
          {
            var latLng = Geolocation.latLng();
            var userId = Meteor.userId();
            if(latLng &&  userId)
            {
              //do something
            }
         }
        });
    

    No need to use interval. I simply put in a file call geolocation.js.