Skip to content

Commit af1c398

Browse files
committed
Make initSelection works asynchronously
1 parent 4ba25b8 commit af1c398

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

select2.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,14 @@
509509
} else if ("tags" in opts) {
510510
opts.query = tags(opts.tags);
511511
opts.createSearchChoice = function (term) { return {id: term, text: term}; };
512-
opts.initSelection = function (element) {
512+
opts.initSelection = function (element, callback) {
513513
var data = [];
514514
$(splitVal(element.val(), ",")).each(function () {
515515
data.push({id: this, text: this});
516516
});
517-
return data;
517+
518+
if ($.isFunction(callback))
519+
callback(data);
518520
};
519521
}
520522
}
@@ -958,26 +960,30 @@
958960
var selected;
959961
if (this.opts.element.val() === "") {
960962
this.updateSelection({id: "", text: ""});
963+
this.close();
964+
this.setPlaceholder();
961965
} else {
962-
selected = this.opts.initSelection.call(null, this.opts.element);
963-
if (selected !== undefined && selected !== null) {
964-
this.updateSelection(selected);
965-
}
966+
var self = this;
967+
this.opts.initSelection.call(null, this.opts.element, function(selected){
968+
if (selected !== undefined && selected !== null) {
969+
self.updateSelection(selected);
970+
self.close();
971+
self.setPlaceholder();
972+
}
973+
});
966974
}
967-
968-
this.close();
969-
this.setPlaceholder();
970975
},
971976

972977
prepareOpts: function () {
973978
var opts = this.parent.prepareOpts.apply(this, arguments);
974979

975980
if (opts.element.get(0).tagName.toLowerCase() === "select") {
976981
// install sthe selection initializer
977-
opts.initSelection = function (element) {
982+
opts.initSelection = function (element, callback) {
978983
var selected = element.find(":selected");
979984
// a single select box always has a value, no need to null check 'selected'
980-
return {id: selected.attr("value"), text: selected.text()};
985+
if ($.isFunction(callback))
986+
callback({id: selected.attr("value"), text: selected.text()});
981987
};
982988
}
983989

@@ -1116,12 +1122,14 @@
11161122

11171123
if (opts.element.get(0).tagName.toLowerCase() === "select") {
11181124
// install sthe selection initializer
1119-
opts.initSelection = function (element) {
1125+
opts.initSelection = function (element,callback) {
11201126
var data = [];
11211127
element.find(":selected").each(function () {
11221128
data.push({id: $(this).attr("value"), text: $(this).text()});
11231129
});
1124-
return data;
1130+
1131+
if ($.isFunction(callback))
1132+
callback(data);
11251133
};
11261134
}
11271135

@@ -1228,18 +1236,21 @@
12281236
var data;
12291237
if (this.opts.element.val() === "") {
12301238
this.updateSelection([]);
1239+
this.close();
1240+
// set the placeholder if necessary
1241+
this.clearSearch();
12311242
}
12321243
if (this.select || this.opts.element.val() !== "") {
1233-
data = this.opts.initSelection.call(null, this.opts.element);
1234-
if (data !== undefined && data !== null) {
1235-
this.updateSelection(data);
1236-
}
1237-
}
1238-
1239-
this.close();
1240-
1241-
// set the placeholder if necessary
1242-
this.clearSearch();
1244+
var self = this;
1245+
this.opts.initSelection.call(null, this.opts.element, function(data){
1246+
if (data !== undefined && data !== null) {
1247+
self.updateSelection(data);
1248+
self.close();
1249+
// set the placeholder if necessary
1250+
self.clearSearch();
1251+
}
1252+
});
1253+
}
12431254
},
12441255

12451256
clearSearch: function () {

0 commit comments

Comments
 (0)