1

I have these two methods that pretty much does the same thing. I would like to improve this by just re-using the other method, something like method overriding in an Object Oriented Programming.

Method 1 Summary: Based on the level, make a post request to a Servlet that returns all the courses for that particular level in JSON format (response). Then load these courses to the combo box ($loadTo). After that pre-select a course provided and show some modal.

function loadCoursesByLevelThenSet(level, course, $loadTo) {
    $.post( ... , ... ,
    function(response) {

        var options = '<option value="">Please select course...</option>';
        for (var i = 0; i < response.length; i++) {
            ...
        }

        $loadTo.html(options);

        $loadTo.val(course);
        $('#modal').modal('show');

    }).fail(function() {

        alert('Something went wrong while loading options for courses. Please try again.');

    });
}

Method 2 Summary: Based on the level, make a post request to a Servlet that returns all the courses for that particular level in JSON format (response), then load these courses to the combo box ($loadTo).

function loadCoursesByLevel(level, $loadTo) {
    $.post( ... , ... ,
    function(response) {

        var options = '<option value="">Please select course...</option>';
        for (var i = 0; i < response.length; i++) {
            ...
        }

        $loadTo.html(options);

    }).fail(function() {

        alert('Something went wrong while loading options for courses. Please try again.');

    });
}

This is what I came up, but it doesn't work correctly:

function loadCoursesByLevelThenSet(level, course, $loadTo) {
    if (loadCoursesByLevel(level, $loadTo) === true) { 

        //after the courses are fully loaded  
        //select a course
        //and show the modal

        $loadTo.val(course);
        $('#modal').modal('show');
    }
}

function loadCoursesByLevel(level, $loadTo) {
    $.post( ... , ... ,
    function(response) {

        var options = '<option value="">Please select course...</option>';
        for (var i = 0; i < response.length; i++) {
            ...
        }

        $loadTo.html(options);

    }).fail(function() {

        alert('Something went wrong while loading options for courses. Please try again.');

    }).done(function() {        

        return true;

    });
}

What am I doing wrong? Is there any way to achieve this? Thank you.

1 Answer 1

1

The problem with your attempted solution is you can't return a value because the ajax is asynchronous.

You could pass a callback to loadCoursesByLevel(), and have it run after it runs its own success code. By checking if the callback is undefined, you can make it optional for more flexibility.

function loadCoursesByLevel(level, $loadTo, callback) {
    $.post( ... , ... ,
    function(response) {
        var options = '<option value="">Please select course...</option>';
        for (var i = 0; i < response.length; i++) {
            options += '<option value="'
                    + response[i].code + '">'
                    + response[i].course + '</option>';
        }
        $loadTo.html(options);

        if(typeof callback != 'undefined'){
            callback(); // <------------- execute callback
        }
    }).fail(function() {
        alert('Something went wrong while loading options for courses. Please try again.');
    });
}

function loadCoursesByLevelThenSet(level, course, $loadTo) {
    loadCoursesByLevel(level, $loadTo, function() { 
        $loadTo.val(course);
        $('#modal').modal('show');
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

You could pass the callback to the done function on the promise -- the return true there does nothing.

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.