1
0
mirror of synced 2025-03-10 22:56:12 +03:00

Merge pull request #168 from alanho/master

Make initSelection works asynchronously
This commit is contained in:
Igor Vaynberg 2012-06-27 21:37:21 -07:00
commit ebcd59fb29

View File

@ -659,12 +659,14 @@
} else if ("tags" in opts) { } else if ("tags" in opts) {
opts.query = tags(opts.tags); opts.query = tags(opts.tags);
opts.createSearchChoice = function (term) { return {id: term, text: term}; }; opts.createSearchChoice = function (term) { return {id: term, text: term}; };
opts.initSelection = function (element) { opts.initSelection = function (element, callback) {
var data = []; var data = [];
$(splitVal(element.val(), ",")).each(function () { $(splitVal(element.val(), ",")).each(function () {
data.push({id: this, text: this}); data.push({id: this, text: this});
}); });
return data;
if ($.isFunction(callback))
callback(data);
}; };
} }
} }
@ -1174,15 +1176,18 @@
var selected; var selected;
if (this.opts.element.val() === "") { if (this.opts.element.val() === "") {
this.updateSelection({id: "", text: ""}); this.updateSelection({id: "", text: ""});
this.close();
this.setPlaceholder();
} else { } else {
selected = this.opts.initSelection.call(null, this.opts.element); var self = this;
if (selected !== undefined && selected !== null) { this.opts.initSelection.call(null, this.opts.element, function(selected){
this.updateSelection(selected); if (selected !== undefined && selected !== null) {
} self.updateSelection(selected);
self.close();
self.setPlaceholder();
}
});
} }
this.close();
this.setPlaceholder();
}, },
// single // single
@ -1191,10 +1196,11 @@
if (opts.element.get(0).tagName.toLowerCase() === "select") { if (opts.element.get(0).tagName.toLowerCase() === "select") {
// install sthe selection initializer // install sthe selection initializer
opts.initSelection = function (element) { opts.initSelection = function (element, callback) {
var selected = element.find(":selected"); var selected = element.find(":selected");
// a single select box always has a value, no need to null check 'selected' // a single select box always has a value, no need to null check 'selected'
return {id: selected.attr("value"), text: selected.text()}; if ($.isFunction(callback))
callback({id: selected.attr("value"), text: selected.text()});
}; };
} }
@ -1342,13 +1348,16 @@
// TODO validate placeholder is a string if specified // TODO validate placeholder is a string if specified
if (opts.element.get(0).tagName.toLowerCase() === "select") { if (opts.element.get(0).tagName.toLowerCase() === "select") {
// install the selection initializer // install sthe selection initializer
opts.initSelection = function (element) { opts.initSelection = function (element,callback) {
var data = []; var data = [];
element.find(":selected").each2(function (i, elm) { element.find(":selected").each2(function (i, elm) {
data.push({id: elm.attr("value"), text: elm.text()}); data.push({id: elm.attr("value"), text: elm.text()});
}); });
return data;
if ($.isFunction(callback))
callback(data);
}; };
} }
@ -1460,18 +1469,21 @@
var data; var data;
if (this.opts.element.val() === "") { if (this.opts.element.val() === "") {
this.updateSelection([]); this.updateSelection([]);
this.close();
// set the placeholder if necessary
this.clearSearch();
} }
if (this.select || this.opts.element.val() !== "") { if (this.select || this.opts.element.val() !== "") {
data = this.opts.initSelection.call(null, this.opts.element); var self = this;
if (data !== undefined && data !== null) { this.opts.initSelection.call(null, this.opts.element, function(data){
this.updateSelection(data); if (data !== undefined && data !== null) {
} self.updateSelection(data);
self.close();
// set the placeholder if necessary
self.clearSearch();
}
});
} }
this.close();
// set the placeholder if necessary
this.clearSearch();
}, },
// multi // multi