Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Added onTagExists callback #60
Great plugin, thanks! We believe the best way to pay for open source is to contribute back.
We found that we wanted to give our users feedback when they tried to add a tag that already existed, but there was no callback for this purpuse. This pull request adds that callback.
It works like the other callbacks, and gives you access to the existing tag that caused the collision. This makes it easy to add an effect to the existing tag.
We repurposed the _isNew function, and renamed it _exists. Instead of returning true/false, it returns the existing tag or false if no such tag was found.
createTag previously returned false if the tag exists or is the empty string. This is where we added the trigger for onTagExists, but the empty string should not trigger onTagExists, so this conditional has been broken up into two conditionals. This has the added benefit of avoiding an unnecessary loop over existing tags for the empty string.
Finally, _exists now checks all .tagit-choice decendants, not just immediate children of tagList. The reason is that some jQuery effects will insert a DIV around the animated element, which interferes with the children lookup. It was possible to insert duplicate tags during e.g. a shake animation.
We hope this callback will be useful for other tag-it users. We've added some documentation to the readme, and an example to examples.html.