Replies: 4 comments 4 replies
-
The answer is in the question ;) |
Beta Was this translation helpful? Give feedback.
-
// Magic: $clipboard
Alpine.magic('clipboard', () => {
return subject => navigator.clipboard.writeText(subject)
})
// Magic: $tooltip
Alpine.magic('tooltip', el => message => {
let instance = tippy(el, { content: message, trigger: 'manual' })
instance.show()
setTimeout(() => {
instance.hide()
setTimeout(() => instance.destroy(), 150)
}, 2000)
}) I am a newbie to javascript. These methods have different interface. How do I call these two methods from another method? |
Beta Was this translation helpful? Give feedback.
-
I understand that. My question is how to define a function that I call once from the directive? How can this new function call the two functions internally, one after the other? |
Beta Was this translation helpful? Give feedback.
-
I appreciate your help @ekwoka. I got it working with the following code: import data from './ssh.json' assert {type: 'json'};
window.dataTable = function () {
return {
items: data,
searchInput: '',
search(value) {
if (value.length > 1) {
const options = {
shouldSort: true,
keys: ['name']
}
const fuse = new Fuse(data, options)
this.items = fuse.search(value).map(element => element.item)
} else {
this.items = data
}
// console.log(this.items.length)
},
tooltip(message, el) {
let instance = tippy(el, { content: message, trigger: 'manual' })
instance.show()
setTimeout(() => {
instance.hide()
setTimeout(() => instance.destroy(), 150)
}, 2000)
},
copyToClipboard(subject, el) {
navigator.clipboard.writeText(subject)
.then(() => {
this.tooltip("Copied!", el)
})
.catch(error => {
console.error('Failed to copy to clipboard: ', error)
})
}
}
}
document.addEventListener('alpine:init', () => {
// Magic: $clipboard
Alpine.magic('clipboard', () => {
return subject => navigator.clipboard.writeText(subject)
})
// Magic: $tooltip
Alpine.magic('tooltip', el => message => {
let instance = tippy(el, { content: message, trigger: 'manual' })
instance.show()
setTimeout(() => {
instance.hide()
setTimeout(() => instance.destroy(), 150)
}, 2000)
})
}) Any suggestions for refactoring is appreciated! |
Beta Was this translation helpful? Give feedback.
-
Currently my code looks like this:
Instead making two separate calls, how can I make it just one call to a method that calls these two magic methods in the right sequence?
Beta Was this translation helpful? Give feedback.
All reactions