Merge pull request #1329 from julienfalque/fix-infinite-change-loop

Fix infinite "change" event triggering loop
This commit is contained in:
Federico Zivolo 2019-05-21 07:22:08 +02:00 committed by GitHub
commit 2f125ee7de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,7 @@ const Autofill = ($ => {
const DATA_KEY = `bmd.${NAME}`; const DATA_KEY = `bmd.${NAME}`;
const JQUERY_NAME = `bmd${NAME.charAt(0).toUpperCase() + NAME.slice(1)}`; const JQUERY_NAME = `bmd${NAME.charAt(0).toUpperCase() + NAME.slice(1)}`;
const JQUERY_NO_CONFLICT = $.fn[JQUERY_NAME]; const JQUERY_NO_CONFLICT = $.fn[JQUERY_NAME];
const LAST_VALUE_DATA_KEY = "bmd.last_value";
const Default = {}; const Default = {};
@ -46,14 +47,20 @@ const Autofill = ($ => {
$("input[type!=checkbox]").each((index, element) => { $("input[type!=checkbox]").each((index, element) => {
let $element = $(element); let $element = $(element);
let initialValue = $element.attr("value"); let previousValue = $element.data(LAST_VALUE_DATA_KEY);
if (initialValue === undefined) { if (previousValue === undefined) {
initialValue = ""; previousValue = $element.attr("value");
}
if (previousValue === undefined) {
previousValue = "";
} }
if ($element.val() && $element.val() !== initialValue) { let currentValue = $element.val();
if (currentValue !== previousValue) {
$element.trigger("change"); $element.trigger("change");
} }
$element.data(LAST_VALUE_DATA_KEY, currentValue);
}); });
}, 100); }, 100);
} }
@ -72,14 +79,20 @@ const Autofill = ($ => {
$inputs.each((index, element) => { $inputs.each((index, element) => {
let $element = $(element); let $element = $(element);
let initialValue = $element.attr("value"); let previousValue = $element.data(LAST_VALUE_DATA_KEY);
if (initialValue === undefined) { if (previousValue === undefined) {
initialValue = ""; previousValue = $element.attr("value");
}
if (previousValue === undefined) {
previousValue = "";
} }
if ($element.val() !== initialValue) { let currentValue = $element.val();
if (currentValue !== previousValue) {
$element.trigger("change"); $element.trigger("change");
} }
$element.data(LAST_VALUE_DATA_KEY, currentValue);
}); });
}, 100); }, 100);
}) })