From 2b6d090c439d962407ab110b85335f338a821eef Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Thu, 8 Apr 2021 21:45:24 +0530 Subject: [PATCH] [fix] Fixed multiple requests made for fetching default template values #423 (#424) The JS code made multiple HTTP request for fetching default template values when any of organization or backend field were updated. Fixed it by not triggering "click" event of sortedm2m-items and manaully updating primary keys of templates using JS. Closes #423 --- .../config/static/config/js/default_templates.js | 5 ++++- openwisp_controller/config/static/config/js/widget.js | 11 +++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/openwisp_controller/config/static/config/js/default_templates.js b/openwisp_controller/config/static/config/js/default_templates.js index b3e981b63..fd786ec8f 100644 --- a/openwisp_controller/config/static/config/js/default_templates.js +++ b/openwisp_controller/config/static/config/js/default_templates.js @@ -42,8 +42,11 @@ django.jQuery(function ($) { $.get(url).done(function (data) { unCheckInputs(); $.each(data.default_templates, function (i, uuid) { - $('input.sortedm2m[value=' + uuid + ']').trigger('click'); + $('input.sortedm2m[value=' + uuid + ']').prop('checked', true); }); + $('input[name="config-0-templates"]').attr('value', data.default_templates.join(',')); + $('.sortedm2m-items:first').trigger('change'); + }); }, bindDefaultTemplateLoading = function (urlSchema) { diff --git a/openwisp_controller/config/static/config/js/widget.js b/openwisp_controller/config/static/config/js/widget.js index 0b389e764..6caa4bad6 100644 --- a/openwisp_controller/config/static/config/js/widget.js +++ b/openwisp_controller/config/static/config/js/widget.js @@ -333,12 +333,6 @@ } }); - // fill device context field with default values of selected templates. - getDefaultValues(true); - - $('.sortedm2m-items').on('change', function() { - getDefaultValues(); - }); // so that other files can use updateContext window.updateContext = updateContext; }; @@ -378,6 +372,11 @@ addConfig.click(bindLoadUi); // otherwise load immediately bindLoadUi(); + // fill device context field with default values of selected templates. + getDefaultValues(true); + $('.sortedm2m-items').on('change', function() { + getDefaultValues(); + }); }); }(django.jQuery));