administrate-field-lazy_belongs_to 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f95651ee7033646d6e9a40c0e8033bc574944b40b7fa53eb184bc4643f19acb8
|
4
|
+
data.tar.gz: 253836f8fb97ac521e0b5650379eb8ec3b9b64deb9482c5e6efe140295c1f683
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '08c4b2efdcfaab90cb9b421e825146e732b2fe6ca6574a3652e2000de70f736f60c56719802aa08de0520bb5483836f198edba001f76d95dcf6dfdb013b24cde'
|
7
|
+
data.tar.gz: af592318beb1fee05ada96708cd17c743fc1be5858e20303b8105f3c4fcd7483dcfd634dc2076620765f3d719640388d45b043a5297722f226edbf640bf817aa
|
data/app/assets/javascripts/administrate-field-lazy_belongs_to/components/lazy_belongs_to.js
CHANGED
@@ -13,6 +13,7 @@ function bindLazyBelongsTos() {
|
|
13
13
|
|
14
14
|
let controller = undefined
|
15
15
|
let lastResult = undefined
|
16
|
+
let lastDebounce = undefined
|
16
17
|
|
17
18
|
function onQuery (event) {
|
18
19
|
const value = event.currentTarget.value
|
@@ -22,47 +23,54 @@ function bindLazyBelongsTos() {
|
|
22
23
|
controller.abort()
|
23
24
|
}
|
24
25
|
|
26
|
+
if (lastDebounce) {
|
27
|
+
clearTimeout(lastDebounce)
|
28
|
+
}
|
29
|
+
|
25
30
|
controller = new AbortController()
|
26
31
|
const { signal } = controller
|
27
|
-
console.log({ hidden: target.value, selected: select.value, query: value })
|
28
|
-
|
29
|
-
fetch(options.url.replace('{q}', value).replace('%7Bq%7D', value), {
|
30
|
-
signal,
|
31
|
-
headers: { Accept: 'application/json' }
|
32
|
-
})
|
33
|
-
.then(r => r.json())
|
34
|
-
.then(r => r.map(e => ({ value: e[options.value], label: e[options.label] })))
|
35
|
-
.then(rs => {
|
36
|
-
const currentResult = JSON.stringify((rs))
|
37
|
-
|
38
|
-
if (lastResult && lastResult === currentResult) {
|
39
|
-
return
|
40
|
-
}
|
41
|
-
|
42
|
-
lastResult = currentResult
|
43
|
-
|
44
|
-
while (select.lastChild) {
|
45
|
-
select.removeChild(select.lastChild);
|
46
|
-
}
|
47
|
-
|
48
|
-
const currentValue = target.value
|
49
|
-
|
50
|
-
rs.forEach(r => {
|
51
|
-
const option = document.createElement('option')
|
52
|
-
option.setAttribute('value', r.value)
|
53
|
-
option.innerText = r.label
|
54
|
-
option.selected = currentValue === r.value
|
55
|
-
select.appendChild(option)
|
56
|
-
})
|
57
32
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
}
|
64
|
-
console.error(error)
|
33
|
+
lastDebounce = setTimeout(() => {
|
34
|
+
lastDebounce = undefined
|
35
|
+
|
36
|
+
fetch(options.url.replace('{q}', value).replace('%7Bq%7D', value), {
|
37
|
+
signal,
|
38
|
+
headers: { Accept: 'application/json' }
|
65
39
|
})
|
40
|
+
.then(r => r.json())
|
41
|
+
.then(r => r.map(e => ({ value: e[options.value], label: e[options.label] })))
|
42
|
+
.then(rs => {
|
43
|
+
const currentResult = JSON.stringify((rs))
|
44
|
+
|
45
|
+
if (lastResult && lastResult === currentResult) {
|
46
|
+
return
|
47
|
+
}
|
48
|
+
|
49
|
+
lastResult = currentResult
|
50
|
+
|
51
|
+
while (select.lastChild) {
|
52
|
+
select.removeChild(select.lastChild);
|
53
|
+
}
|
54
|
+
|
55
|
+
const currentValue = target.value
|
56
|
+
|
57
|
+
rs.forEach(r => {
|
58
|
+
const option = document.createElement('option')
|
59
|
+
option.setAttribute('value', r.value)
|
60
|
+
option.innerText = r.label
|
61
|
+
option.selected = currentValue === r.value
|
62
|
+
select.appendChild(option)
|
63
|
+
})
|
64
|
+
|
65
|
+
select.setAttribute('size', "" + Math.max(2, Math.min(Number(select.getAttribute('data-max-size')), rs.length)))
|
66
|
+
})
|
67
|
+
.catch(error => {
|
68
|
+
if (error.name === 'AbortError') {
|
69
|
+
return /* ignore, this is an aborted promise */
|
70
|
+
}
|
71
|
+
console.error(error)
|
72
|
+
})
|
73
|
+
}, 250)
|
66
74
|
}
|
67
75
|
|
68
76
|
function showPopout() {
|