Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Implement onBeforeAdd callback #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

etdsoft
Copy link

@etdsoft etdsoft commented Mar 26, 2011

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

@loopj
Copy link
Owner

loopj commented Mar 27, 2011

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

…he server side combined with the use of onBeforeFilter
@etdsoft
Copy link
Author

etdsoft commented Mar 27, 2011

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?

@loopj loopj force-pushed the master branch 2 times, most recently from fd2316b to 8a8390e Compare August 3, 2023 06:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants