Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nested requests #395

Closed
suchow opened this issue Jul 26, 2016 · 2 comments
Closed

Remove nested requests #395

suchow opened this issue Jul 26, 2016 · 2 comments
Assignees

Comments

@suchow
Copy link
Member

suchow commented Jul 26, 2016

Consider the nested HTTP requests in the following snippet from questionnaire.js:

submit_responses = function() {
    if (lock===false) {
        lock=true;
        reqwest({
            url: "/question/" + participant_id,
            method: 'post',
            type: 'json',
            data: {
                question: "engagement",
                number: 1,
                response: $("#engagement").val()
            },
            success: function (resp) {
                reqwest({
                    url: "/question/" + participant_id,
                    method: 'post',
                    type: 'json',
                    data: {
                        question: "difficulty",
                        number: 2,
                        response: $("#difficulty").val()
                    },
                    success: function(resp) {
                        submit_assignment();
                    },
                    error: function (err) {
                        err_response = JSON.parse(err.response);
                        if (err_response.hasOwnProperty('html')) {
                            $('body').html(err_response.html);
                        }
                    }
                });
            },
            error: function (err) {
                console.log(err);
                err_response = JSON.parse(err.response);
                if (err_response.hasOwnProperty('html')) {
                    $('body').html(err_response.html);
                }
            }
        });
    }
};

Because the second HTTP request does not depend on the first, it is unnecessary to nest them in this way. It would be simpler to write a for loop over the questions and post them one by one.

This appears in several places elsewhere.

@suchow
Copy link
Member Author

suchow commented Jul 26, 2016

Okay, @thomasmorgan pointed out a problem with the above solution, which is that moving to the next page can interrupt the HTTP requests. Instead of a loop, we should write a recursive function that automatically extracts all the ids of the questions and recursively runs through the list, posting each response.

@DallingerBot
Copy link
Collaborator

This issue was moved to Dallinger/Dallinger#76

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants