Algolia/Autocomplete:给处理程序进行选择
我要设置多个在ES6类中的字段,我想提取 OnSelect
处理程序,因此我可以在HTML节点中使用一些data_attributes,但是我不知道该怎么做。
这是我的JS课程:
import { autocomplete } from '@algolia/autocomplete-js';
export default class AutocompleteSetUp {
init() {
document.querySelectorAll('.entity-autocomplete').forEach(elem => {
this.autocomplete_elem_setup(elem);
});
}
autocompleteSelectionHandler(some params) {
console.log('I NEED TO PERFORM SOME ACTIONS HERE');
}
autocomplete_elem_setup(htmlNode) {
autocomplete({
container: htmlNode,
// DYNAMIC SEARCH
getSources({ query }) {
return fetch(
`/artists.json?query=${query}`
)
.then((response) => response.json())
.then((data) => {
return [
{
sourceId: `artists`,
getItems() {
return data;
},
getItemInputValue({ item }) {
return item.name;
},
// ...
templates: {
header() {
return 'Suggestions';
},
item({ item }) {
return `Artist: ${item.name}`;
},
footer() {
return 'Powered by Algolia Autocomplete';
},
},
onSelect({ item }) {
console.log(item);
console.log('container:', htmlNode );
console.log('source:', source );
// Here I'm able to perform some actions
// But I need to extract this code in another function in the class:
// How can I call autocompleteSelectionHandler ?
// This doesn't work:
this.autocompleteSelectionHandler(some_params);
},
},
];
});
},
});
}
}
老实说,我也很想确切的其他处理程序以“ clean_codeness”(例如 intem
in 模板
),但是我需要调用几种方法在 OnSelect
中,我真的别无选择。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论