Algolia/Autocomplete:给处理程序进行选择

发布于 2025-01-20 08:02:48 字数 2307 浏览 2 评论 0 原文

我要设置多个在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 中,我真的别无选择。

I'm setting up multiple Algolia/autocomplete fields in a ES6 class, I would like to extract the onSelect handler, so I could use some data_attributes in the HTML node, But I can't figure out how to do it.

Here is my JS class:

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);

              },


            },
          ];
        });
      },
    });
  }
}

To be honest, I would love to also exact other handlers for "clean_codeness" (like item in templates), but I need to call several methods in onSelect so I really have no choice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文