0

I'm using the plugin http://www.jquery-steps.com/ and per documentation I should return true to allow a user to pass to the next slide. I'm verifying if the zip code exists in our database. Only when the server verifies that the ZIPCode exists in our database the user is allowed to click next.

What I tried

onStepChanging: function (event, currentIndex, newIndex) {
    if(currentIndex == 2){
        getZipCode($("#zip").val()).done(function(returnedData){
            if(returnedData.length > 0){
                return true;
            }else{
                setHelperText($("#zip_helper"), 'Zip Code doesn\'t exists in our database. Pick the closest one please');
            }
        }).fail(function () {
            setHelperText($("#zip_helper"),'There was an error connecting to our servers');
        });

        return false;
        }
    }
    function getZipCode(zip_code) {
    return $.get('../ajax/zip-code', {
        q: zip_code
    });
}

What I expect

User may be able to click next only when the ZIPCode exists in our database.

From what I have read online the problem is that I'm returning return false at the end thus the script is always returning false. How can I return true inside of .done() to be applicable to the outside scope?

Bruno Francisco
  • 3,841
  • 4
  • 31
  • 61

1 Answers1

0

Return true inside synchronous ajax call

Maybe you meant asynchronous.

How can I return true inside of .done() to be applicable to the outside scope?

The short answer is that you can't. If their library supported promise-driven callbacks, that would be an option here, but after glancing at their docs, it doesn't look like they do. You'll need to restructure your code or use another library. Without being familiar with all of your code, or the library you're using, I can't suggest details, but I would suggest restructuring such that you have the necessary data from your server before you ever load the steps plugin.

Mike Bell
  • 1,356
  • 11
  • 9