Skip to content

Commit 6d5b0a6

Browse files
committed
Add getters and setters for options
This also gets an object set up that handles the default options.
1 parent d0fe07f commit 6d5b0a6

10 files changed

Lines changed: 352 additions & 138 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ define('select2/data/ajax',[
710710
'jquery'
711711
], function (ArrayAdapter, Utils, $) {
712712
function AjaxAdapter ($element, options) {
713-
this.ajaxOptions = options.options.ajax;
713+
this.ajaxOptions = options.get('ajax');
714714

715715
this.processResults = this.ajaxOptions.processResults ||
716716
function (results) {
@@ -810,7 +810,7 @@ define('select2/dropdown/search',[
810810
return Search;
811811
});
812812

813-
define('select2/options',[
813+
define('select2/defaults',[
814814
'./results',
815815

816816
'./selection/single',
@@ -827,32 +827,72 @@ define('select2/options',[
827827
], function (ResultsList, SingleSelection, MultipleSelection, Utils,
828828
SelectData, ArrayData, AjaxData,
829829
Dropdown, Search) {
830-
function Options (options) {
831-
this.options = options;
830+
function Defaults () {
831+
this.reset();
832+
}
832833

833-
if (options.ajax) {
834-
this.dataAdapter = this.dataAdapter || AjaxData;
835-
} else if (options.data) {
836-
this.dataAdapter = this.dataAdapter || ArrayData;
837-
} else {
838-
this.dataAdapter = this.dataAdapter || SelectData;
834+
Defaults.prototype.apply = function (options) {
835+
options = $.extend({}, options, this.defaults);
836+
837+
if (options.dataAdapter == null) {
838+
if (options.ajax) {
839+
options.dataAdapter = AjaxData;
840+
} else if (options.data) {
841+
options.dataAdapter = ArrayData;
842+
} else {
843+
options.dataAdapter = SelectData;
844+
}
845+
}
846+
847+
if (options.resultsAdapter == null) {
848+
options.resultsAdapter = ResultsList;
839849
}
840850

841-
var SearchableDropdown = Utils.Decorate(Dropdown, Search);
851+
if (options.dropdownAdapter == null) {
852+
var SearchableDropdown = Utils.Decorate(Dropdown, Search);
842853

843-
this.resultsAdapter = ResultsList;
844-
this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
845-
this.selectionAdapter = options.selectionAdapter;
854+
options.dropdownAdapter = SearchableDropdown;
855+
}
846856

847-
if (this.selectionAdapter == null) {
848-
if (this.options.multiple) {
849-
this.selectionAdapter = MultipleSelection;
857+
if (options.selectionAdapter == null) {
858+
if (options.multiple) {
859+
options.selectionAdapter = MultipleSelection;
850860
} else {
851-
this.selectionAdapter = SingleSelection;
861+
options.selectionAdapter = SingleSelection;
852862
}
853863
}
864+
865+
return options;
866+
};
867+
868+
Defaults.prototype.reset = function () {
869+
this.defaults = { };
870+
};
871+
872+
var defaults = new Defaults();
873+
874+
return defaults;
875+
});
876+
877+
define('select2/options',[
878+
'./defaults'
879+
], function (Defaults) {
880+
function Options (options) {
881+
this.options = Defaults.apply(options);
854882
}
855883

884+
Options.prototype.fromElement = function ($e) {
885+
return this;
886+
};
887+
888+
Options.prototype.get = function (key) {
889+
return this.options[key];
890+
};
891+
892+
Options.prototype.set = function (key, val) {
893+
this.options[key] = val;
894+
};
895+
856896
return Options;
857897
});
858898

@@ -874,7 +914,8 @@ define('select2/core',[
874914

875915
// Set up containers and adapters
876916

877-
this.data = new this.options.dataAdapter($element, this.options);
917+
var DataAdapter = this.options.get('dataAdapter');
918+
this.data = new DataAdapter($element, this.options);
878919

879920
var $container = this.render();
880921
this.$container = $container;
@@ -883,22 +924,24 @@ define('select2/core',[
883924

884925
$container.width($element.outerWidth(false));
885926

886-
this.selection = new this.options.selectionAdapter($element, this.options);
927+
var SelectionAdapter = this.options.get('selectionAdapter');
928+
this.selection = new SelectionAdapter($element, this.options);
887929

888930
var $selectionContainer = $container.find('.selection');
889931
var $selection = this.selection.render();
890932

891933
$selectionContainer.append($selection);
892934

893-
this.dropdown = new this.options.dropdownAdapter($element, this.options);
935+
var DropdownAdapter = this.options.get('dropdownAdapter');
936+
this.dropdown = new DropdownAdapter($element, this.options);
894937

895938
var $dropdownContainer = $container.find('.dropdown-wrapper');
896939
var $dropdown = this.dropdown.render();
897940

898941
$dropdownContainer.append($dropdown);
899942

900-
this.results = new this.options.resultsAdapter(
901-
$element, this.options, this.data);
943+
var ResultsAdapter = this.options.get('resultsAdapter');
944+
this.results = new ResultsAdapter($element, this.options, this.data);
902945

903946
var $resultsContainer = $dropdown.find('.results');
904947
var $results = this.results.render();

dist/js/select2.amd.js

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ define('select2/data/ajax',[
710710
'jquery'
711711
], function (ArrayAdapter, Utils, $) {
712712
function AjaxAdapter ($element, options) {
713-
this.ajaxOptions = options.options.ajax;
713+
this.ajaxOptions = options.get('ajax');
714714

715715
this.processResults = this.ajaxOptions.processResults ||
716716
function (results) {
@@ -810,7 +810,7 @@ define('select2/dropdown/search',[
810810
return Search;
811811
});
812812

813-
define('select2/options',[
813+
define('select2/defaults',[
814814
'./results',
815815

816816
'./selection/single',
@@ -827,32 +827,72 @@ define('select2/options',[
827827
], function (ResultsList, SingleSelection, MultipleSelection, Utils,
828828
SelectData, ArrayData, AjaxData,
829829
Dropdown, Search) {
830-
function Options (options) {
831-
this.options = options;
830+
function Defaults () {
831+
this.reset();
832+
}
832833

833-
if (options.ajax) {
834-
this.dataAdapter = this.dataAdapter || AjaxData;
835-
} else if (options.data) {
836-
this.dataAdapter = this.dataAdapter || ArrayData;
837-
} else {
838-
this.dataAdapter = this.dataAdapter || SelectData;
834+
Defaults.prototype.apply = function (options) {
835+
options = $.extend({}, options, this.defaults);
836+
837+
if (options.dataAdapter == null) {
838+
if (options.ajax) {
839+
options.dataAdapter = AjaxData;
840+
} else if (options.data) {
841+
options.dataAdapter = ArrayData;
842+
} else {
843+
options.dataAdapter = SelectData;
844+
}
845+
}
846+
847+
if (options.resultsAdapter == null) {
848+
options.resultsAdapter = ResultsList;
839849
}
840850

841-
var SearchableDropdown = Utils.Decorate(Dropdown, Search);
851+
if (options.dropdownAdapter == null) {
852+
var SearchableDropdown = Utils.Decorate(Dropdown, Search);
842853

843-
this.resultsAdapter = ResultsList;
844-
this.dropdownAdapter = options.dropdownAdapter || SearchableDropdown;
845-
this.selectionAdapter = options.selectionAdapter;
854+
options.dropdownAdapter = SearchableDropdown;
855+
}
846856

847-
if (this.selectionAdapter == null) {
848-
if (this.options.multiple) {
849-
this.selectionAdapter = MultipleSelection;
857+
if (options.selectionAdapter == null) {
858+
if (options.multiple) {
859+
options.selectionAdapter = MultipleSelection;
850860
} else {
851-
this.selectionAdapter = SingleSelection;
861+
options.selectionAdapter = SingleSelection;
852862
}
853863
}
864+
865+
return options;
866+
};
867+
868+
Defaults.prototype.reset = function () {
869+
this.defaults = { };
870+
};
871+
872+
var defaults = new Defaults();
873+
874+
return defaults;
875+
});
876+
877+
define('select2/options',[
878+
'./defaults'
879+
], function (Defaults) {
880+
function Options (options) {
881+
this.options = Defaults.apply(options);
854882
}
855883

884+
Options.prototype.fromElement = function ($e) {
885+
return this;
886+
};
887+
888+
Options.prototype.get = function (key) {
889+
return this.options[key];
890+
};
891+
892+
Options.prototype.set = function (key, val) {
893+
this.options[key] = val;
894+
};
895+
856896
return Options;
857897
});
858898

@@ -874,7 +914,8 @@ define('select2/core',[
874914

875915
// Set up containers and adapters
876916

877-
this.data = new this.options.dataAdapter($element, this.options);
917+
var DataAdapter = this.options.get('dataAdapter');
918+
this.data = new DataAdapter($element, this.options);
878919

879920
var $container = this.render();
880921
this.$container = $container;
@@ -883,22 +924,24 @@ define('select2/core',[
883924

884925
$container.width($element.outerWidth(false));
885926

886-
this.selection = new this.options.selectionAdapter($element, this.options);
927+
var SelectionAdapter = this.options.get('selectionAdapter');
928+
this.selection = new SelectionAdapter($element, this.options);
887929

888930
var $selectionContainer = $container.find('.selection');
889931
var $selection = this.selection.render();
890932

891933
$selectionContainer.append($selection);
892934

893-
this.dropdown = new this.options.dropdownAdapter($element, this.options);
935+
var DropdownAdapter = this.options.get('dropdownAdapter');
936+
this.dropdown = new DropdownAdapter($element, this.options);
894937

895938
var $dropdownContainer = $container.find('.dropdown-wrapper');
896939
var $dropdown = this.dropdown.render();
897940

898941
$dropdownContainer.append($dropdown);
899942

900-
this.results = new this.options.resultsAdapter(
901-
$element, this.options, this.data);
943+
var ResultsAdapter = this.options.get('resultsAdapter');
944+
this.results = new ResultsAdapter($element, this.options, this.data);
902945

903946
var $resultsContainer = $dropdown.find('.results');
904947
var $results = this.results.render();

0 commit comments

Comments
 (0)