Fix champ address on repetitions

This commit is contained in:
Paul Chavard 2019-02-07 11:01:21 +01:00
parent a0a1ce11c8
commit bb5c90c579

View file

@ -38,13 +38,28 @@ function source(url) {
} }
addEventListener('turbolinks:load', function() { addEventListener('turbolinks:load', function() {
autocompleteSetup();
});
addEventListener('ajax:success', function() {
autocompleteSetup();
});
function autocompleteSetup() {
for (let { type, url } of sources) { for (let { type, url } of sources) {
for (let target of document.querySelectorAll(selector(type))) { for (let element of document.querySelectorAll(selector(type))) {
let select = autocomplete(target, options, [source(url)]); if (!element.dataset.autocompleteInitialized) {
select.on('autocomplete:selected', ({ target }, suggestion) => { autocompleteInitializeElement(element, url);
fire(target, 'autocomplete:select', suggestion); }
select.autocomplete.setVal(suggestion.label);
});
} }
} }
}); }
function autocompleteInitializeElement(element, url) {
const select = autocomplete(element, options, [source(url)]);
select.on('autocomplete:selected', ({ target }, suggestion) => {
fire(target, 'autocomplete:select', suggestion);
select.autocomplete.setVal(suggestion.label);
});
element.dataset.autocompleteInitialized = true;
}