1

What do the variables result and status refer to in this? Where do they come from? How does origin, destination and travel mode get passed into the function? Where does the result of the function go?

$('#directions-form').submit(function(e) {
  $('#error').hide();
  ds.route({
    origin: $('#from').val(),
    destination: $('#to').val(),
    travelMode: $('#mode').val()
  }, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      fitBounds = true;
      dr.setDirections(result);
    }
    else {
      $('#error').text(status).show();
    }
    recalcHeight();
  });
  e.preventDefault();
  return false;
});

3 Answers 3

2

They are the parameters of the success callback function.

route method when invoked with proper variables fires the callback asynchronously when the response is a success. The parameters are part of the callback as part of the method invocation.

So you can go ahead and replace them with any variable name you want which will can be used inside the function closure.

 }, function(a, b) {  // Will work to 
Sign up to request clarification or add additional context in comments.

Comments

1

This function is being called with two parameters:

ds.route()

The first parameter is an object with some values in it:

{
    origin: $('#from').val(),
    destination: $('#to').val(),
    travelMode: $('#mode').val()
}

The second parameter is a function:

function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        fitBounds = true;
        dr.setDirections(result);
    }
    else {
        $('#error').text(status).show();
    }
    recalcHeight();
}

Note that in JavaScript a function is an object like any other, and can be passed around like a variable. Since it's being passed into ds.route() as a variable, the function itself isn't being executed yet.

Internally, ds.route() is using the first parameter which has the values (origin, destination, travelMode) to do, well, something. It doesn't matter what, it's just whatever that function does.

Then, when it's done, it's going to execute the second parameter, which is the function. When it executes that function, it's going to pass two values into it as parameters. Those values will end up being the result and status variables used within the function.

To illustrate, you could do something as simple as this:

function doSomething(value, callback) {
    if (value == 1) {
        alert("The value is 1");
        callback(2)
    }
}

doSomething(1, function (value) {
    if (value == 2) {
        alert("The value is 2");
    }
});

This defines a function which takes two arguments, and expects them to be a number and a function. If the number equals 1, it executes that function. This function is then called with the value 1 and a function, which will be executed as a callback.

3 Comments

Excellent! Well articulated.
What purpose is function(e) serving at the very start of the function?
@benknighthorse: That is the function which is being passed (as a parameter) to the .submit() function, which is assigning it as a handler for the submit event on the element with id directions-form. When the submit event for that element is triggered, it will call the supplied function and pass it a value representing the event being triggered, which will be what's in e.
0

When ds.route is called with route object as it's sole argument, it generates a result and a status. These are passed as the arguments to its callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.