Skip to content

Commit

Permalink
a bit of cleanup and better error handling for options
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaynberg committed May 7, 2012
1 parent a6edacc commit e5f628b
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,40 +435,34 @@
prepareOpts: function (opts) {
var element, select, idKey;

element = opts.element;

if (element.get(0).tagName.toLowerCase() === "select") {
this.select = select = opts.element;
}

if (select) {
// these options are not allowed when attached to a select because they are picked up off the element itself
$.each(["id", "multiple", "ajax", "query", "createSearchChoice", "initSelection", "data", "tags"], function () {
if (this in opts) {
throw new Error("Option '" + this + "' is not allowed for Select2 when attached to a <select> element.");
}
});
}

opts = $.extend({}, {
formatResult: function (data) { return data.text; },
formatSelection: function (data) { return data.text; },
formatNoMatches: function () { return "No matches found"; },
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
minimumResultsForSearch: 0,
minimumInputLength: 0,
id: function (e) { return e.id; }
}, opts);

if (typeof(opts.id) !== "function") {
idKey = opts.id;
opts.id = function (e) { return e[idKey]; }
}

element = opts.element;

if (element.get(0).tagName.toLowerCase() === "select") {
this.select = select = opts.element;
}

// TODO add missing validation logic
if (select) {
/*$.each(["multiple", "ajax", "query", "minimumInputLength"], function () {
if (this in opts) {
throw "Option '" + this + "' is not allowed for Select2 when attached to a select element";
}
});*/
this.opts = opts = $.extend({}, {
miniumInputLength: 0
}, opts);
} else {
this.opts = opts = $.extend({}, {
miniumInputLength: 0
}, opts);
opts.id = function (e) { return e[idKey]; };
}

if (select) {
Expand Down

0 comments on commit e5f628b

Please sign in to comment.