Skip to content

Commit ebcd59f

Browse files
committed
Merge pull request select2#168 from alanho/master
Make initSelection works asynchronously
2 parents a7bd800 + e8083a6 commit ebcd59f

1 file changed

Lines changed: 36 additions & 24 deletions

File tree

select2.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,14 @@
659659
} else if ("tags" in opts) {
660660
opts.query = tags(opts.tags);
661661
opts.createSearchChoice = function (term) { return {id: term, text: term}; };
662-
opts.initSelection = function (element) {
662+
opts.initSelection = function (element, callback) {
663663
var data = [];
664664
$(splitVal(element.val(), ",")).each(function () {
665665
data.push({id: this, text: this});
666666
});
667-
return data;
667+
668+
if ($.isFunction(callback))
669+
callback(data);
668670
};
669671
}
670672
}
@@ -1174,15 +1176,18 @@
11741176
var selected;
11751177
if (this.opts.element.val() === "") {
11761178
this.updateSelection({id: "", text: ""});
1179+
this.close();
1180+
this.setPlaceholder();
11771181
} else {
1178-
selected = this.opts.initSelection.call(null, this.opts.element);
1179-
if (selected !== undefined && selected !== null) {
1180-
this.updateSelection(selected);
1181-
}
1182+
var self = this;
1183+
this.opts.initSelection.call(null, this.opts.element, function(selected){
1184+
if (selected !== undefined && selected !== null) {
1185+
self.updateSelection(selected);
1186+
self.close();
1187+
self.setPlaceholder();
1188+
}
1189+
});
11821190
}
1183-
1184-
this.close();
1185-
this.setPlaceholder();
11861191
},
11871192

11881193
// single
@@ -1191,10 +1196,11 @@
11911196

11921197
if (opts.element.get(0).tagName.toLowerCase() === "select") {
11931198
// install sthe selection initializer
1194-
opts.initSelection = function (element) {
1199+
opts.initSelection = function (element, callback) {
11951200
var selected = element.find(":selected");
11961201
// a single select box always has a value, no need to null check 'selected'
1197-
return {id: selected.attr("value"), text: selected.text()};
1202+
if ($.isFunction(callback))
1203+
callback({id: selected.attr("value"), text: selected.text()});
11981204
};
11991205
}
12001206

@@ -1342,13 +1348,16 @@
13421348
// TODO validate placeholder is a string if specified
13431349

13441350
if (opts.element.get(0).tagName.toLowerCase() === "select") {
1345-
// install the selection initializer
1346-
opts.initSelection = function (element) {
1351+
// install sthe selection initializer
1352+
opts.initSelection = function (element,callback) {
1353+
13471354
var data = [];
13481355
element.find(":selected").each2(function (i, elm) {
13491356
data.push({id: elm.attr("value"), text: elm.text()});
13501357
});
1351-
return data;
1358+
1359+
if ($.isFunction(callback))
1360+
callback(data);
13521361
};
13531362
}
13541363

@@ -1460,18 +1469,21 @@
14601469
var data;
14611470
if (this.opts.element.val() === "") {
14621471
this.updateSelection([]);
1472+
this.close();
1473+
// set the placeholder if necessary
1474+
this.clearSearch();
14631475
}
14641476
if (this.select || this.opts.element.val() !== "") {
1465-
data = this.opts.initSelection.call(null, this.opts.element);
1466-
if (data !== undefined && data !== null) {
1467-
this.updateSelection(data);
1468-
}
1469-
}
1470-
1471-
this.close();
1472-
1473-
// set the placeholder if necessary
1474-
this.clearSearch();
1477+
var self = this;
1478+
this.opts.initSelection.call(null, this.opts.element, function(data){
1479+
if (data !== undefined && data !== null) {
1480+
self.updateSelection(data);
1481+
self.close();
1482+
// set the placeholder if necessary
1483+
self.clearSearch();
1484+
}
1485+
});
1486+
}
14751487
},
14761488

14771489
// multi

0 commit comments

Comments
 (0)