1
0
mirror of synced 2024-11-22 13:06:08 +03:00

Better compatibility with array data

When using array data, and an option is selected, the data that is
attached to the DOM element will be run through `item` and should
have any private, automatically generated attributes added and
merged with it.
This commit is contained in:
Kevin Brown 2014-10-19 21:03:48 -04:00
parent 72ae0da879
commit 9e130956fc
8 changed files with 93 additions and 32 deletions

View File

@ -1044,10 +1044,11 @@ define('select2/data/select',[
define('select2/data/array',[
'./select',
'../utils'
], function (SelectAdapter, Utils) {
'../utils',
'jquery'
], function (SelectAdapter, Utils, $) {
function ArrayAdapter ($element, options) {
this.data = options.options.data;
this.data = options.get('data');
ArrayAdapter.__super__.constructor.call(this, $element, options);
}
@ -1078,7 +1079,16 @@ define('select2/data/array',[
$option.text(data.text);
$option.val(data.id);
$option.data('data', data);
$option.prop('disabled', data.disabled || false);
// Get any automatically generated data values
var detectedData = this.item($option);
// Merge it with the already present data
var combinedData = $.extend({}, data, detectedData);
// Override the option's data with the combined data
$option.data('data', combinedData);
return $option;
};

View File

@ -1044,10 +1044,11 @@ define('select2/data/select',[
define('select2/data/array',[
'./select',
'../utils'
], function (SelectAdapter, Utils) {
'../utils',
'jquery'
], function (SelectAdapter, Utils, $) {
function ArrayAdapter ($element, options) {
this.data = options.options.data;
this.data = options.get('data');
ArrayAdapter.__super__.constructor.call(this, $element, options);
}
@ -1078,7 +1079,16 @@ define('select2/data/array',[
$option.text(data.text);
$option.val(data.id);
$option.data('data', data);
$option.prop('disabled', data.disabled || false);
// Get any automatically generated data values
var detectedData = this.item($option);
// Merge it with the already present data
var combinedData = $.extend({}, data, detectedData);
// Override the option's data with the combined data
$option.data('data', combinedData);
return $option;
};

View File

@ -10582,10 +10582,11 @@ define('select2/data/select',[
define('select2/data/array',[
'./select',
'../utils'
], function (SelectAdapter, Utils) {
'../utils',
'jquery'
], function (SelectAdapter, Utils, $) {
function ArrayAdapter ($element, options) {
this.data = options.options.data;
this.data = options.get('data');
ArrayAdapter.__super__.constructor.call(this, $element, options);
}
@ -10616,7 +10617,16 @@ define('select2/data/array',[
$option.text(data.text);
$option.val(data.id);
$option.data('data', data);
$option.prop('disabled', data.disabled || false);
// Get any automatically generated data values
var detectedData = this.item($option);
// Merge it with the already present data
var combinedData = $.extend({}, data, detectedData);
// Override the option's data with the combined data
$option.data('data', combinedData);
return $option;
};

File diff suppressed because one or more lines are too long

18
dist/js/select2.js vendored
View File

@ -1473,10 +1473,11 @@ define('select2/data/select',[
define('select2/data/array',[
'./select',
'../utils'
], function (SelectAdapter, Utils) {
'../utils',
'jquery'
], function (SelectAdapter, Utils, $) {
function ArrayAdapter ($element, options) {
this.data = options.options.data;
this.data = options.get('data');
ArrayAdapter.__super__.constructor.call(this, $element, options);
}
@ -1507,7 +1508,16 @@ define('select2/data/array',[
$option.text(data.text);
$option.val(data.id);
$option.data('data', data);
$option.prop('disabled', data.disabled || false);
// Get any automatically generated data values
var detectedData = this.item($option);
// Merge it with the already present data
var combinedData = $.extend({}, data, detectedData);
// Override the option's data with the combined data
$option.data('data', combinedData);
return $option;
};

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,10 @@
define([
'./select',
'../utils'
], function (SelectAdapter, Utils) {
'../utils',
'jquery'
], function (SelectAdapter, Utils, $) {
function ArrayAdapter ($element, options) {
this.data = options.options.data;
this.data = options.get('data');
ArrayAdapter.__super__.constructor.call(this, $element, options);
}
@ -34,7 +35,16 @@ define([
$option.text(data.text);
$option.val(data.id);
$option.data('data', data);
$option.prop('disabled', data.disabled || false);
// Get any automatically generated data values
var detectedData = this.item($option);
// Merge it with the already present data
var combinedData = $.extend({}, data, detectedData);
// Override the option's data with the combined data
$option.data('data', combinedData);
return $option;
};

View File

@ -60,7 +60,7 @@ test('current works with existing selections', function (assert) {
assert.equal(
val.length,
1,
'There should only be one existing selection'
'There should only be one existing selection.'
);
var option = val[0];
@ -68,13 +68,13 @@ test('current works with existing selections', function (assert) {
assert.equal(
option.id,
'3',
'The id should be equal to the value of the option tag'
'The id should be equal to the value of the option tag.'
);
assert.equal(
option.text,
'Three',
'The text should be equal to the text of the option tag'
'The text should be equal to the text of the option tag.'
);
});
});
@ -90,13 +90,24 @@ test('current works with selected data', function (assert) {
});
data.current(function (val) {
assert.deepEqual(
val,
[{
id: '2',
text: '2'
}],
'The text and id should match the selected array data.'
assert.equal(
val.length,
1,
'There should only be one option selected.'
);
var option = val[0];
assert.equal(
option.id,
'2',
'The id should match the original id from the array.'
);
assert.equal(
option.text,
'2',
'The text should match the original text from the array.'
);
});
});