Skip to content

Commit e5f628b

Browse files
committed
a bit of cleanup and better error handling for options
1 parent a6edacc commit e5f628b

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

select2.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -435,40 +435,34 @@
435435
prepareOpts: function (opts) {
436436
var element, select, idKey;
437437

438+
element = opts.element;
439+
440+
if (element.get(0).tagName.toLowerCase() === "select") {
441+
this.select = select = opts.element;
442+
}
443+
444+
if (select) {
445+
// these options are not allowed when attached to a select because they are picked up off the element itself
446+
$.each(["id", "multiple", "ajax", "query", "createSearchChoice", "initSelection", "data", "tags"], function () {
447+
if (this in opts) {
448+
throw new Error("Option '" + this + "' is not allowed for Select2 when attached to a <select> element.");
449+
}
450+
});
451+
}
452+
438453
opts = $.extend({}, {
439454
formatResult: function (data) { return data.text; },
440455
formatSelection: function (data) { return data.text; },
441456
formatNoMatches: function () { return "No matches found"; },
442457
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
443458
minimumResultsForSearch: 0,
459+
minimumInputLength: 0,
444460
id: function (e) { return e.id; }
445461
}, opts);
446462

447463
if (typeof(opts.id) !== "function") {
448464
idKey = opts.id;
449-
opts.id = function (e) { return e[idKey]; }
450-
}
451-
452-
element = opts.element;
453-
454-
if (element.get(0).tagName.toLowerCase() === "select") {
455-
this.select = select = opts.element;
456-
}
457-
458-
// TODO add missing validation logic
459-
if (select) {
460-
/*$.each(["multiple", "ajax", "query", "minimumInputLength"], function () {
461-
if (this in opts) {
462-
throw "Option '" + this + "' is not allowed for Select2 when attached to a select element";
463-
}
464-
});*/
465-
this.opts = opts = $.extend({}, {
466-
miniumInputLength: 0
467-
}, opts);
468-
} else {
469-
this.opts = opts = $.extend({}, {
470-
miniumInputLength: 0
471-
}, opts);
465+
opts.id = function (e) { return e[idKey]; };
472466
}
473467

474468
if (select) {

0 commit comments

Comments
 (0)