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

Ajax Whitelist with "enforceWhitelist" setting enabled #465

Closed
as247 opened this issue Apr 10, 2020 · 1 comment
Closed

Ajax Whitelist with "enforceWhitelist" setting enabled #465

as247 opened this issue Apr 10, 2020 · 1 comment

Comments

@as247
Copy link

as247 commented Apr 10, 2020

How to enforce with ajax whitelist.
I'm using this for user picker, so must enforceWhitelist, but whitelist load from ajax, then previous selected value become invalid and get removed

Thank you.

@yairEO
Copy link
Owner

yairEO commented Apr 10, 2020

Excellent question!

You can merge the currently added tags (Array), tagify.value, to a dynamically loaded whitelist

Example:

var tagify = new Tagify(inputElm, {
	whitelist: initialTagsArray // see "important" section below
})

// listen to custom "input" event
tagify.on('input', onTagifyInput)

// get tags from server (via AJAX for example)
// example of async function 
// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
function async onTagifyInput(){
    // clear current whitelist
	tagify.settings.whitelist.length = 0; // reset current whitelist
	// show loader & hide suggestions dropdown (if opened)
    tagify.loading(true).dropdown.hide.call(tagify)

	var newWhitelist = await getWhitelistFromServer()

	// replace tagify "whitelist" array values with new values 
    // and add back the ones already choses as Tags
    tagify.settings.whitelist.push(...newWhitelist, ...tagify.value)

    // render the suggestions dropdown
    tagify.loading(false).dropdown.show.call(tagify, e.detail.value);
}

Important:

Make sure the initial whitelist supplied to Tagify, when it first being loaded on the page, includes the user's selected tags, else, if Tagify was loaded with initial tags, cannot know these tags are allowed.

I could, theoretically, change the source-code to always allow tags which were predefined when Tagify was initialized, but I would prefer to be very strict about it, and always follow only what the whitelist setting allows and nothing else, if enforceWhitelist is set to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants