jquery-tokeninput
jquery-tokeninput copied to clipboard
Implement onBeforeAdd callback
Hi,
Don't know if you've followed the discussions in the tracker around issue #11:
https://github.com/loopj/jquery-tokeninput/issues#issue/11
I just implemented the onBeforeAdd callback trying to follow your code conventions and thought it would be nice to contribute it back. I didn't change the existing onAdd to a onAfterAdd to avoid breaking compatibility, but maybe that's something to consider in the future.
Thanks for the awesome plugin.
Daniel
Hey etd, thanks for the pull request, I plan on merging this in soon.
Could you give me an example of how you are using the onBeforeAdd callback to implement adding of new tokens?
Thanks
I just committed to my copy of the repo two files in the examples/ directory.
Following @ryanb's suggestion in the tracker, the server detects whether the user-provided name already exists or not. If it doesn't it adds a new item to the JSON array { "id": "CREATE_foo_END", :name => "Create "foo""}.
This is presented in the drop-down list. If the user clicks on it and submits the form, the sever will get a list of IDs and a CREATE_foo_END item too. Which means that the item can be created on the server side.
One of the examples uses the onBeforeAdd method to modify the item's name before the plugin adds it to the list by removing the 'Create ' part.
For instance, if using the plugin to handle object tagging. Say I want to add a new tag "foo" (that doesn't exist on the server). The server won't find the tag and will include a 'Create "foo"' item in the JSON response. But when I click on the item, I want "foo" to be the tag name and not 'Create "foo"', so I use the onBeforeAdd callback to alter the item's name before inserting it in the field.
Does this make sense?