diff --git a/src/js/select2/data/array.js b/src/js/select2/data/array.js index b8899662..a43c5a6d 100644 --- a/src/js/select2/data/array.js +++ b/src/js/select2/data/array.js @@ -4,15 +4,19 @@ define([ 'jquery' ], function (SelectAdapter, Utils, $) { function ArrayAdapter ($element, options) { - var data = options.get('data') || []; + this._dataToConvert = options.get('data') || []; ArrayAdapter.__super__.constructor.call(this, $element, options); - - this.addOptions(this.convertToOptions(data)); } Utils.Extend(ArrayAdapter, SelectAdapter); + ArrayAdapter.prototype.bind = function (container, $container) { + ArrayAdapter.__super__.bind.call(this, container, $container); + + this.addOptions(this.convertToOptions(this._dataToConvert)); + }; + ArrayAdapter.prototype.select = function (data) { var $option = this.$element.find('option').filter(function (i, elm) { return elm.value == data.id.toString(); diff --git a/tests/data/array-tests.js b/tests/data/array-tests.js index d0dead68..e0c8a266 100644 --- a/tests/data/array-tests.js +++ b/tests/data/array-tests.js @@ -71,6 +71,9 @@ test('current gets default for single', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + data.current(function (val) { assert.equal( val.length, @@ -93,6 +96,9 @@ test('current gets default for multiple', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('')); + data.current(function (val) { assert.equal( val.length, @@ -107,6 +113,9 @@ test('current works with existing selections', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('')); + $select.val(['One']); data.current(function (val) { @@ -137,6 +146,9 @@ test('current works with selected data', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('')); + data.select({ id: '2', text: '2' @@ -170,6 +182,9 @@ test('select works for single', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('')); + assert.equal( $select.val(), 'default', @@ -193,6 +208,9 @@ test('multiple sets the value', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('')); + assert.ok( $select.val() == null || $select.val().length == 0, 'nothing should be selected' @@ -211,6 +229,9 @@ test('multiple adds to the old value', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('')); + $select.val(['One']); assert.deepEqual($select.val(), ['One']); @@ -228,6 +249,9 @@ test('option tags are automatically generated', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('')); + assert.equal( $select.find('option').length, 4, @@ -235,11 +259,32 @@ test('option tags are automatically generated', function (assert) { ); }); +test('automatically generated option tags have a result id', function (assert) { + var $select = $('#qunit-fixture .single-empty'); + + var data = new ArrayData($select, arrayOptions); + + var container = new MockContainer(); + data.bind(container, $('')); + + data.select({ + id: 'default' + }); + + assert.ok( + Utils.GetData($select.find(':selected')[0], 'data')._resultId, + '