I am using select2 as a tagging input , but i am stumped when it comes to handling the creation of new tags and getting the new tag id's back into the select2
this question is closely related Select2.js: why is id the same as text on change for removed?
and you can see from his jsfiddle http://jsfiddle.net/7e8Pa/ , when text is not found in the createSearchChoice, new option is created, id:-1, text:term, then in the on change event, altering that id to 5
I need to be able to submit a $.post
to the server, and get an id back instead of using a static 5
The problem is, if i submit a post in the createsearchoption, every keystroke not found in the tags tabe creates a new tag, and attempting the post in the on change event , i assume the change event finishes before the ajax returns
.on("change", function(e) {
log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed}));
if (e.added) { // if its got an add obj
if (isNumeric(e.added.id)){
//if its not an existing tag , it passes a string instead of an id
// so just do a regular add
add(e.added.id);
} else {
//get the term text, post to server, return the new tag id
$.post('handlers/tags.ashx',
{ operation:"select2createtag",
text: $.trim(e.added.id.substring(3, e.added.id.length))} ,
function(data){
add(data.id);
});
};