From 11f147f2d94c8aff729590183e24337b2c076c86 Mon Sep 17 00:00:00 2001 From: Richard Mathot Date: Thu, 15 Jan 2015 17:40:30 +0100 Subject: [PATCH] [FIX] survey: prevent pointless AJAX request Avoid creation of doublon survey_user_input entries when a user loads the landing page of a survey (/survey/start/... route). Due to some strange spec, jQuery.ajax() function called with "undefined" URL will do an extra call to the URL of the webpage where the script lies (http://api.jquery.com/jQuery.ajax/). Now, we check that URL is not "undefined" to avoid those calls. By the way, this problem probably happened in every page that had survey.js in its assets... (correct loading of survey.js is fixed in saas-6 at 4dd5dbb28974b3f0d9cbcc9b502aab2d83b5e6f3, this fix is complementary.) This commit fixes #3032 and closes #3337 #3338 #3092. --- addons/survey/static/src/js/survey.js | 44 +++++++++++++++------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/addons/survey/static/src/js/survey.js b/addons/survey/static/src/js/survey.js index 65df4ef3b7b..0d6e2aa341e 100644 --- a/addons/survey/static/src/js/survey.js +++ b/addons/survey/static/src/js/survey.js @@ -80,33 +80,37 @@ $(document).ready(function () { // Pre-filling of the form with previous answers function prefill(){ - var prefill_def = $.ajax(prefill_controller, {dataType: "json"}) - .done(function(json_data){ - _.each(json_data, function(value, key){ - the_form.find(".form-control[name=" + key + "]").val(value); - the_form.find("input[name^=" + key + "]").each(function(){ - $(this).val(value); + if (! _.isUndefined(prefill_controller)) { + var prefill_def = $.ajax(prefill_controller, {dataType: "json"}) + .done(function(json_data){ + _.each(json_data, function(value, key){ + the_form.find(".form-control[name=" + key + "]").val(value); + the_form.find("input[name^=" + key + "]").each(function(){ + $(this).val(value); + }); }); + }) + .fail(function(){ + console.warn("[survey] Unable to load prefill data"); }); - }) - .fail(function(){ - console.warn("[survey] Unable to load prefill data"); - }); - return prefill_def; + return prefill_def; + } } // Display score if quiz correction mode function display_scores(){ - var score_def = $.ajax(scores_controller, {dataType: "json"}) - .done(function(json_data){ - _.each(json_data, function(value, key){ - the_form.find("span[data-score-question=" + key + "]").text("Your score: " + value); + if (! _.isUndefined(scores_controller)) { + var score_def = $.ajax(scores_controller, {dataType: "json"}) + .done(function(json_data){ + _.each(json_data, function(value, key){ + the_form.find("span[data-score-question=" + key + "]").text("Your score: " + value); + }); + }) + .fail(function(){ + console.warn("[survey] Unable to load score data"); }); - }) - .fail(function(){ - console.warn("[survey] Unable to load score data"); - }); - return score_def; + return score_def; + } } // Parameters for form submission