Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup: unselection of items from AJAX data sources with non-string identifiers #6241

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/js/select2/data/ajax.js
Expand Up @@ -66,6 +66,9 @@ define([

function request () {
var $request = options.transport(options, function (data) {
if (data.results) {
data.results = data.results.map(AjaxAdapter.prototype._normalizeItem);
}
var results = self.processResults(data, params);
jayaddison marked this conversation as resolved.
Show resolved Hide resolved

if (self.options.get('debug') && window.console && console.error) {
Expand Down
4 changes: 0 additions & 4 deletions src/js/select2/data/select.js
Expand Up @@ -70,10 +70,6 @@ define([
SelectAdapter.prototype.unselect = function (data) {
var self = this;

if (!this.$element.prop('multiple')) {
return;
}

data.selected = false;

if (
Expand Down
12 changes: 6 additions & 6 deletions src/js/select2/results.js
Expand Up @@ -455,12 +455,12 @@ define([
var data = Utils.GetData(this, 'data');

if ($this.hasClass('select2-results__option--selected')) {
if (self.options.get('multiple')) {
self.trigger('unselect', {
originalEvent: evt,
data: data
});
} else {
self.trigger('unselect', {
originalEvent: evt,
data: data
});

if (!self.options.get('multiple')) {
self.trigger('close', {
originalEvent: evt,
data: data
Expand Down
1 change: 1 addition & 0 deletions tests/integration-jq1.html
Expand Up @@ -10,6 +10,7 @@

<script src="vendor/qunit-1.23.1.js" type="text/javascript"></script>
<script src="vendor/jquery-1.12.4.js" type="text/javascript"></script>
<script src="vendor/jquery.mockjax-2.6.0.js" type="text/javascript"></script>
<script src="../dist/js/select2.full.js" type="text/javascript"></script>

<script src="helpers.js" type="text/javascript"></script>
Expand Down
1 change: 1 addition & 0 deletions tests/integration-jq2.html
Expand Up @@ -10,6 +10,7 @@

<script src="vendor/qunit-1.23.1.js" type="text/javascript"></script>
<script src="vendor/jquery-2.2.4.js" type="text/javascript"></script>
<script src="vendor/jquery.mockjax-2.6.0.js" type="text/javascript"></script>
<script src="../dist/js/select2.full.js" type="text/javascript"></script>

<script src="helpers.js" type="text/javascript"></script>
Expand Down
1 change: 1 addition & 0 deletions tests/integration-jq3.html
Expand Up @@ -10,6 +10,7 @@

<script src="vendor/qunit-1.23.1.js" type="text/javascript"></script>
<script src="vendor/jquery-3.4.1.js" type="text/javascript"></script>
<script src="vendor/jquery.mockjax-2.6.0.js" type="text/javascript"></script>
<script src="../dist/js/select2.full.js" type="text/javascript"></script>

<script src="helpers.js" type="text/javascript"></script>
Expand Down
57 changes: 57 additions & 0 deletions tests/integration/select2-methods.js
Expand Up @@ -137,3 +137,60 @@ test('multiple value matches the jquery value', function (assert) {
'The values should match the jquery values'
);
});

test('selection and clearing of data from ajax source', function (assert) {
var asyncDone = assert.async();

var dataURL = 'http://127.0.0.1/test';
$.mockjax({
url: dataURL,
responseText: {results: [{id: 6128, text: '6128'}]},
logging: 1
});

var $container = $('#qunit-fixture');
var $select = $('<select></select>');
$container.append($select);

var select = new Select2($select, {ajax: {url: dataURL}});

assert.equal(
$select.find(':selected').length,
0,
'No items should be selected'
);

// Open the dropdown menu, to perform an AJAX request
select.selection.trigger('query', {term: '6128'});

var selectionStatus = null;
select.on('results:all', function() {

// First call: select a result from the dropdown menu
if (selectionStatus === null) {

$('.select2-results__option').trigger('mouseup');
assert.equal(
$select.find(':selected').length,
1,
'One item should be selected'
);

// Trigger a second call
selectionStatus = true;
select.selection.trigger('query', {term: '6128'});

// Second call: unselect the previously-selected item
} else if (selectionStatus == true) {

$('.select2-results__option[aria-selected=true]').trigger('mouseup');
assert.equal(
$select.find(':selected').length,
0,
'The previously-selected item should have been unselected'
);

asyncDone();
}
});
});