I'm using select2 to display a dynamic list of names. The names are fetch from the server as the user types, by resemblance, but in case a name is not found, I display a "Suggest" link which adds the name to the database.
The "Suggest" link appears in the select's notification area (where "Searching..." appears when querying the server). When the user presses "Suggest", a request is made that, as said, adds the suggested name to the database. Now, when that request responds, I want the name to be added to the select2's list, selected, and the drop box closed.
I'm nothing being successful in this endeavor =) I tried using 'val', but I'm either not doing it right or that's not the way to go. Here's a code excerpt from what I have right now:
$('.' + id + '-select2', nextFrame).select2({
    ajax: {
        url: url,
        dataType: 'json',
        results: function(data) {
            var results = [], it;
            for (it in data) {
                results.push({
                    id: data[it]['pid'],
                    text: data[it]['name']
                });
            }
            return { results: results }
        },
        data: function(text) {
            return { name: text }
        }
    },
    formatNoMatches: function(text) {
        // Add the AJAX behavior to the anchor, but do it asynchronously
        // This way we give time to select2 to render the layout before we configure it
        setTimeout(function() {
            $('#' + id + '-ajax-anchor').on('click', function(e) {
                var target = $(e.target);
                $.ajax({
                    url: target.attr('href'),
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        name: text
                    },
                })
                .done(function(response) {
                    /*
                        Need help here !!
                        1) Add the response to the list
                        2) Select it
                        3) Close
                    */
                })
                ;
                e.preventDefault();
            });
        }, 1);
        return "No match, <a id='" + id + "-ajax-anchor' href='" + url + "'>suggest</a>";
    },
});