jQuery Tag-It Access to instance from function
I want to disallow new tags.
$('#my').tagit({
tagSource: ['One', 'Two'],
beforeTagAdded: function (e, ui) {
return (-1 !== ???.indexOf(ui.tagLabel))
}
});
How I can to get instance tagSource in beforeTagAdded function?
Or maybe need to pass tagSource or availableTags in preprocessTag function?
if(this.options.preprocessTag) {
value = this.options.preprocessTag(value, this.options.tagSource);
}
I needed this too. This is how I did it:
$(function() {
var availableTags = ['one', 'two'];
$('#myTagUL').tagit({
singleField: true,
singleFieldNode: '#myHiddenInputForPostingToServer',
availableTags: availableTags,
beforeTagAdded: function(event, ui) {
return availableTags.indexOf(ui.tagLabel) > -1;
}
});
});
Because the plugin doesn't expose the options, you can only pass them when initializing and you can no longer access them afterwards (explained here - search for "options outside"). That's why I need to declare an availableTags variable outside the tagit() call.
You might not need the first two options. What the beforeTag does is return a boolean to indicate if the tag should be added.
Check out the documentation on beforeTagAdded.
Yeah, I know this solution. But I want to use multiple tacit instances. And separately availableTags is not uncomfortably.
Well, in my case I'm using an ASP.NET UserControl, so what it essentially adds to the page is:
$(function() {
var availableTags = ['one', 'two'];
// etc.
});
$(function() {
var availableTags = ['apple', 'orange'];
// etc.
});
These variable will not conflict with each other as they stay in the scope of their function. So you could use $(function() {}) separately for each tagit call. I don't believe this has any effect on jQuery's performance.
Facing the same issue.