Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple init Tagify #1336

Closed
Kossin7 opened this issue Apr 30, 2024 · 1 comment
Closed

Multiple init Tagify #1336

Kossin7 opened this issue Apr 30, 2024 · 1 comment

Comments

@Kossin7
Copy link

Kossin7 commented Apr 30, 2024

Hi, is best of the best component)
There are two questions.

1. Multiple init Tagify.
There are many Tagify elements on the page, also dynamic ones. Writing initialization code for each is not very good.
Wrote and sampled the function:

function initTagify(el, data = {}){

    let opt = {
        delimiters: '?'
    }

    // example
    if(el.getAttribute('data-one') == '1'){
        opt.mode = 'select';
    }

    // set some option 
    if(el.getAttribute('data-bla_bla') == '1'){
        opt.some_prop = 'some_value';
    }

    // init
    let tagifyItem = new Tagify(el, opt);

    if(el.getAttribute('data-suggest') != ''){
        tagifyItem.on('input', function (e){
            tagifyItem.whitelist = null;
            requestTagifySuggest(tagifyItem,{'e' : e, 'type' : 'getProducts' })
        });
    }

}

then on dom load

document.addEventListener('DOMContentLoaded', () => {
    if(document.querySelector('.productFiltersList .productFilterListItem')){
        document.querySelectorAll('.productFiltersList .productFilterListItem').forEach((el) => {
            initTagify(el.querySelector('input'))
        })
    }
}

If write to input have error in console
image

I probably approached the problem incorrectly or wrote the code incorrectly, please tell me how to organize such an idea correctly?

2. Second question , reinit Tagify

@yairEO
Copy link
Owner

yairEO commented May 2, 2024

To detect dynamically added DOM nodes, please read my answer here: https://stackoverflow.com/a/14570614/104380

I assume you have no idea when are those inputs which are supposed to be Tagified are added, because you would have had them Tagified right after that.

Regarding your initTagify function, it is strange to put the input event callback inside it, as it might be garbage-collected. And on top of that, you are creating a new callback function every time instead of using the same one for all of your Tagify inputs..

You do not need to access the tagifyItem variable inside the input event callback because you should have multiple Tagify instances using the same callback, and so you can access the instance (in the callback function) like so:

function initTagify(el, data = {}){

    let opt = {
        delimiters: '?'
    }

    // example
    if(el.getAttribute('data-one') == '1'){
        opt.mode = 'select';
    }

    // set some option 
    if(el.getAttribute('data-bla_bla') == '1'){
        opt.some_prop = 'some_value';
    }

    // init
    let tagifyItem = new Tagify(el, opt);

    if(el.getAttribute('data-suggest'))
        tagifyItem.on('input', onTagifyInput);
}

function onTagifyInput(e) {
    tagify = e.detail.tagify
    tagify.whitelist = null
    requestTagifySuggest(tagify, {'e' : e, 'type' : 'getProducts' })
}

@yairEO yairEO closed this as completed May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants