Skip to content

Commit e04188c

Browse files
committed
Added backwards compatibility for initSelection
Now if the old `initSelection` method is used, a warning will be triggered in the console informing the user that it has now been changed to `DataAdapter.current`, and they should create a custom data adapter instead. We will still only call `initSelection` once, and then default back to the old data adapter for calls after that. This allows for mostly transparent support between versions. This adds tests to ensure that `initSelection` operates the same as it previously did, but now patches the `current` method of the default data adapter.
1 parent 286b838 commit e04188c

9 files changed

Lines changed: 349 additions & 3 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,6 +3528,40 @@ define('select2/defaults',[
35283528
Tokenizer
35293529
);
35303530
}
3531+
3532+
if (options.initSelection != null) {
3533+
if (console && console.warn) {
3534+
console.warn(
3535+
'Select2: The `initSelection` option has been deprecated in favor' +
3536+
' of a custom data adapter that overrides the `current` method. ' +
3537+
'This method is now called multiple times instead of a single ' +
3538+
'time when the instance is initialized.'
3539+
);
3540+
}
3541+
3542+
var oldCurrent = options.dataAdapter.prototype.current;
3543+
var newCurrent = function (callback) {
3544+
var self = this;
3545+
3546+
if (this._isInitialized) {
3547+
oldCurrent.call(this, callback);
3548+
3549+
return;
3550+
}
3551+
3552+
options.initSelection.call(null, this.$element, function (data) {
3553+
self._isInitialized = true;
3554+
3555+
if (!$.isArray(data)) {
3556+
data = [data];
3557+
}
3558+
3559+
callback(data);
3560+
});
3561+
};
3562+
3563+
options.dataAdapter.prototype.current = newCurrent;
3564+
}
35313565
}
35323566

35333567
if (options.resultsAdapter == null) {

dist/js/select2.amd.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,6 +3528,40 @@ define('select2/defaults',[
35283528
Tokenizer
35293529
);
35303530
}
3531+
3532+
if (options.initSelection != null) {
3533+
if (console && console.warn) {
3534+
console.warn(
3535+
'Select2: The `initSelection` option has been deprecated in favor' +
3536+
' of a custom data adapter that overrides the `current` method. ' +
3537+
'This method is now called multiple times instead of a single ' +
3538+
'time when the instance is initialized.'
3539+
);
3540+
}
3541+
3542+
var oldCurrent = options.dataAdapter.prototype.current;
3543+
var newCurrent = function (callback) {
3544+
var self = this;
3545+
3546+
if (this._isInitialized) {
3547+
oldCurrent.call(this, callback);
3548+
3549+
return;
3550+
}
3551+
3552+
options.initSelection.call(null, this.$element, function (data) {
3553+
self._isInitialized = true;
3554+
3555+
if (!$.isArray(data)) {
3556+
data = [data];
3557+
}
3558+
3559+
callback(data);
3560+
});
3561+
};
3562+
3563+
options.dataAdapter.prototype.current = newCurrent;
3564+
}
35313565
}
35323566

35333567
if (options.resultsAdapter == null) {

dist/js/select2.full.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13063,6 +13063,40 @@ define('select2/defaults',[
1306313063
Tokenizer
1306413064
);
1306513065
}
13066+
13067+
if (options.initSelection != null) {
13068+
if (console && console.warn) {
13069+
console.warn(
13070+
'Select2: The `initSelection` option has been deprecated in favor' +
13071+
' of a custom data adapter that overrides the `current` method. ' +
13072+
'This method is now called multiple times instead of a single ' +
13073+
'time when the instance is initialized.'
13074+
);
13075+
}
13076+
13077+
var oldCurrent = options.dataAdapter.prototype.current;
13078+
var newCurrent = function (callback) {
13079+
var self = this;
13080+
13081+
if (this._isInitialized) {
13082+
oldCurrent.call(this, callback);
13083+
13084+
return;
13085+
}
13086+
13087+
options.initSelection.call(null, this.$element, function (data) {
13088+
self._isInitialized = true;
13089+
13090+
if (!$.isArray(data)) {
13091+
data = [data];
13092+
}
13093+
13094+
callback(data);
13095+
});
13096+
};
13097+
13098+
options.dataAdapter.prototype.current = newCurrent;
13099+
}
1306613100
}
1306713101

1306813102
if (options.resultsAdapter == null) {

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3956,6 +3956,40 @@ define('select2/defaults',[
39563956
Tokenizer
39573957
);
39583958
}
3959+
3960+
if (options.initSelection != null) {
3961+
if (console && console.warn) {
3962+
console.warn(
3963+
'Select2: The `initSelection` option has been deprecated in favor' +
3964+
' of a custom data adapter that overrides the `current` method. ' +
3965+
'This method is now called multiple times instead of a single ' +
3966+
'time when the instance is initialized.'
3967+
);
3968+
}
3969+
3970+
var oldCurrent = options.dataAdapter.prototype.current;
3971+
var newCurrent = function (callback) {
3972+
var self = this;
3973+
3974+
if (this._isInitialized) {
3975+
oldCurrent.call(this, callback);
3976+
3977+
return;
3978+
}
3979+
3980+
options.initSelection.call(null, this.$element, function (data) {
3981+
self._isInitialized = true;
3982+
3983+
if (!$.isArray(data)) {
3984+
data = [data];
3985+
}
3986+
3987+
callback(data);
3988+
});
3989+
};
3990+
3991+
options.dataAdapter.prototype.current = newCurrent;
3992+
}
39593993
}
39603994

39613995
if (options.resultsAdapter == null) {

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/defaults.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,40 @@ define([
9292
Tokenizer
9393
);
9494
}
95+
96+
if (options.initSelection != null) {
97+
if (console && console.warn) {
98+
console.warn(
99+
'Select2: The `initSelection` option has been deprecated in favor' +
100+
' of a custom data adapter that overrides the `current` method. ' +
101+
'This method is now called multiple times instead of a single ' +
102+
'time when the instance is initialized.'
103+
);
104+
}
105+
106+
var oldCurrent = options.dataAdapter.prototype.current;
107+
var newCurrent = function (callback) {
108+
var self = this;
109+
110+
if (this._isInitialized) {
111+
oldCurrent.call(this, callback);
112+
113+
return;
114+
}
115+
116+
options.initSelection.call(null, this.$element, function (data) {
117+
self._isInitialized = true;
118+
119+
if (!$.isArray(data)) {
120+
data = [data];
121+
}
122+
123+
callback(data);
124+
});
125+
};
126+
127+
options.dataAdapter.prototype.current = newCurrent;
128+
}
95129
}
96130

97131
if (options.resultsAdapter == null) {

tests/options/deprecated-tests.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
module('Options - Deprecated - initSelection');
2+
3+
var $ = require('jquery');
4+
var Options = require('select2/options');
5+
6+
test('converted into dataAdapter.current', function (assert) {
7+
expect(5);
8+
9+
var $test = $('<select></select>');
10+
var called = false;
11+
12+
var options = new Options({
13+
initSelection: function ($element, callback) {
14+
called = true;
15+
16+
callback([{
17+
id: '1',
18+
text: '2'
19+
}]);
20+
}
21+
}, $test);
22+
23+
assert.ok(!called, 'initSelection should not have been called');
24+
25+
var DataAdapter = options.get('dataAdapter');
26+
var data = new DataAdapter($test, options);
27+
28+
data.current(function (data) {
29+
assert.equal(
30+
data.length,
31+
1,
32+
'There should have only been one object selected'
33+
);
34+
35+
var item = data[0];
36+
37+
assert.equal(
38+
item.id,
39+
'1',
40+
'The id should have been set by initSelection'
41+
);
42+
43+
assert.equal(
44+
item.text,
45+
'2',
46+
'The text should have been set by initSelection'
47+
);
48+
});
49+
50+
assert.ok(called, 'initSelection should have been called');
51+
});
52+
53+
test('single option converted to array automatically', function (assert) {
54+
expect(2);
55+
56+
var $test = $('<select></select>');
57+
var called = false;
58+
59+
var options = new Options({
60+
initSelection: function ($element, callback) {
61+
called = true;
62+
63+
callback({
64+
id: '1',
65+
text: '2'
66+
});
67+
}
68+
}, $test);
69+
70+
var DataAdapter = options.get('dataAdapter');
71+
var data = new DataAdapter($test, options);
72+
73+
data.current(function (data) {
74+
assert.ok(
75+
$.isArray(data),
76+
'The data should have been converted to an array'
77+
);
78+
});
79+
80+
assert.ok(called, 'initSelection should have been called');
81+
});
82+
83+
test('only called once', function (assert) {
84+
expect(8);
85+
86+
var $test = $('<select><option value="3" selected>4</option></select>');
87+
var called = 0;
88+
89+
var options = new Options({
90+
initSelection: function ($element, callback) {
91+
called++;
92+
93+
callback([{
94+
id: '1',
95+
text: '2'
96+
}]);
97+
}
98+
}, $test);
99+
100+
var DataAdapter = options.get('dataAdapter');
101+
var data = new DataAdapter($test, options);
102+
103+
data.current(function (data) {
104+
assert.equal(
105+
data.length,
106+
1,
107+
'There should have only been a single option'
108+
);
109+
110+
var item = data[0];
111+
112+
assert.equal(
113+
item.id,
114+
'1',
115+
'The id should match the one given by initSelection'
116+
);
117+
118+
assert.equal(
119+
item.text,
120+
'2',
121+
'The text should match the one given by initSelection'
122+
);
123+
});
124+
125+
assert.equal(
126+
called,
127+
1,
128+
'initSelection should have been called'
129+
);
130+
131+
data.current(function (data) {
132+
assert.equal(
133+
data.length,
134+
1,
135+
'There should have only been a single option'
136+
);
137+
138+
var item = data[0];
139+
140+
assert.equal(
141+
item.id,
142+
'3',
143+
'The id should match the value given in the DOM'
144+
);
145+
146+
assert.equal(
147+
item.text,
148+
'4',
149+
'The text should match the text given in the DOM'
150+
);
151+
});
152+
153+
assert.equal(
154+
called,
155+
1,
156+
'initSelection should have only been called once'
157+
);
158+
});

tests/options/deprecated.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="../vendor/qunit-1.14.0.css" type="text/css" />
5+
<link rel="stylesheet" href="../../dist/css/select2.css" type="text/css" />
6+
</head>
7+
<body>
8+
<div id="qunit"></div>
9+
<div id="qunit-fixture"></div>
10+
11+
<script src="../vendor/qunit-1.14.0.js" type="text/javascript"></script>
12+
<script src="../../vendor/almond-0.2.9.js" type="text/javascript"></script>
13+
<script src="../../vendor/jquery-2.1.0.js" type="text/javascript"></script>
14+
<script src="../../dist/js/select2.amd.js" type="text/javascript"></script>
15+
16+
<script src="deprecated-tests.js" type="text/javascript"></script>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)