Skip to content

Commit 61a231d

Browse files
committed
Added back select2("data")
In the past, `select2("data")` allowed you to retrieve the currently selected data objects. Read-only support has been added back for this, which maps to the `current` method of the data adapter. This will only work for data adapters which allow for the synchronous retrieval of the current data, which is the case for all of the default data adapters. You could also previously overwrite the currently selected data objects by passing in an argument to `select2("data")`. As this dealt directly with the internals, and required a considerable amount of work to synchronize it, it has been removed. A warning will now be triggered if the method is called with additional elements, and the `val` method should be used instead. This closes select2#2938.
1 parent 8f8140e commit 61a231d

7 files changed

Lines changed: 87 additions & 2 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4531,6 +4531,23 @@ define('select2/core',[
45314531
this.$element.prop('disabled', disabled);
45324532
};
45334533

4534+
Select2.prototype.data = function () {
4535+
if (arguments.length > 0 && console && console.warn) {
4536+
console.warn(
4537+
'Select2: Data can no longer be set using `select2("data")`. You ' +
4538+
'should consider setting the value instead using `$element.val()`.'
4539+
);
4540+
}
4541+
4542+
var data = [];
4543+
4544+
this.dataAdpater.current(function (currentData) {
4545+
data = currentData;
4546+
});
4547+
4548+
return data;
4549+
};
4550+
45344551
Select2.prototype.val = function (args) {
45354552
if (console && console.warn) {
45364553
console.warn(

dist/js/select2.amd.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4531,6 +4531,23 @@ define('select2/core',[
45314531
this.$element.prop('disabled', disabled);
45324532
};
45334533

4534+
Select2.prototype.data = function () {
4535+
if (arguments.length > 0 && console && console.warn) {
4536+
console.warn(
4537+
'Select2: Data can no longer be set using `select2("data")`. You ' +
4538+
'should consider setting the value instead using `$element.val()`.'
4539+
);
4540+
}
4541+
4542+
var data = [];
4543+
4544+
this.dataAdpater.current(function (currentData) {
4545+
data = currentData;
4546+
});
4547+
4548+
return data;
4549+
};
4550+
45344551
Select2.prototype.val = function (args) {
45354552
if (console && console.warn) {
45364553
console.warn(

dist/js/select2.full.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,6 +4969,23 @@ define('select2/core',[
49694969
this.$element.prop('disabled', disabled);
49704970
};
49714971

4972+
Select2.prototype.data = function () {
4973+
if (arguments.length > 0 && console && console.warn) {
4974+
console.warn(
4975+
'Select2: Data can no longer be set using `select2("data")`. You ' +
4976+
'should consider setting the value instead using `$element.val()`.'
4977+
);
4978+
}
4979+
4980+
var data = [];
4981+
4982+
this.dataAdpater.current(function (currentData) {
4983+
data = currentData;
4984+
});
4985+
4986+
return data;
4987+
};
4988+
49724989
Select2.prototype.val = function (args) {
49734990
if (console && console.warn) {
49744991
console.warn(

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,6 +4969,23 @@ define('select2/core',[
49694969
this.$element.prop('disabled', disabled);
49704970
};
49714971

4972+
Select2.prototype.data = function () {
4973+
if (arguments.length > 0 && console && console.warn) {
4974+
console.warn(
4975+
'Select2: Data can no longer be set using `select2("data")`. You ' +
4976+
'should consider setting the value instead using `$element.val()`.'
4977+
);
4978+
}
4979+
4980+
var data = [];
4981+
4982+
this.dataAdpater.current(function (currentData) {
4983+
data = currentData;
4984+
});
4985+
4986+
return data;
4987+
};
4988+
49724989
Select2.prototype.val = function (args) {
49734990
if (console && console.warn) {
49744991
console.warn(

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/core.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,23 @@ define([
408408
this.$element.prop('disabled', disabled);
409409
};
410410

411+
Select2.prototype.data = function () {
412+
if (arguments.length > 0 && console && console.warn) {
413+
console.warn(
414+
'Select2: Data can no longer be set using `select2("data")`. You ' +
415+
'should consider setting the value instead using `$element.val()`.'
416+
);
417+
}
418+
419+
var data = [];
420+
421+
this.dataAdpater.current(function (currentData) {
422+
data = currentData;
423+
});
424+
425+
return data;
426+
};
427+
411428
Select2.prototype.val = function (args) {
412429
if (console && console.warn) {
413430
console.warn(

0 commit comments

Comments
 (0)