Sam Mason
Sam Mason

Reputation: 1037

Possible to evaluate an if block to more than just true or false in meteor

I have a helper function for a meteor template and would ideally like to have 3 different outcomes appear. One for a correct outcome another for an inccorect outcome and one if the user denies access for the browser to access it location, see below:

Template.header.created = function() {
navigator.geolocation.getCurrentPosition(success_callback,error_callback);

function success_callback(p){
    // Building Latitude = 51.522206
    // Building Longitude = -0.078305
    var lat = parseFloat(p.coords.latitude);
    var lon = parseFloat(p.coords.longitude);

  if( lat >= 51.521606 && lat <= 51.522606 && lon >= -0.078805  && lon <=  -0.077705 ) {
    Session.set("locationCheck",true);
  } else {
    Session.set("locationCheck",false);
  }
}

function error_callback(p){
     Session.set("locationCheck",false);
}
}

As you can see it depends on whether the callback is successful or not.

Upvotes: 0

Views: 66

Answers (2)

Anis D
Anis D

Reputation: 761

You can also change the Boolean type to String and defines a generic helper with Template.registerHelper

Upvotes: 0

legacy
legacy

Reputation: 241

If you are trying to pass more than boolean logic from a function, you should probably use a string or a number return. this can be acheived by:

Template.header.created = function() {
navigator.geolocation.getCurrentPosition(success_callback,error_callback);

function success_callback(p){
// Building Latitude = 51.522206
// Building Longitude = -0.078305
var lat = parseFloat(p.coords.latitude);
var lon = parseFloat(p.coords.longitude);

if( lat >= 51.521606 && lat <= 51.522606 && lon >= -0.078805  && lon <=  -0.077705 ) {
Session.set("locationCheck",0);
} else {
Session.set("locationCheck",1);
}
}

function error_callback(p){
 Session.set("locationCheck",2);
}
}

and work from there.

Upvotes: 0

Related Questions