Edit the default initSelection function to pass all tag properties
The function will now pass all properties of the objects passed as tags to the constructor, not just the "id" and "text" properties. Because a tag is not always just an id and a text, just like when you provide the items with the "data" object instead.
This makes sure these properties are always available at the runtime of formatSelection() or formatResult(), which is not currently the case when the field is repopulated on a triggered "change" event.
My code looks like this :
$el.select2({
...,
tags: [{
//id
id: 1,
//the string that will matched against the user's query
text: 'martin smith [email protected]'
//the label of the item that will be shown if this tag is selected
label: 'M. Smith',
}],
formatSelection: function(object, container){
return object.label || object.text;
},
//right now I have to override the default function:
initSelection: function(){...}
});