|
509 | 509 | } else if ("tags" in opts) { |
510 | 510 | opts.query = tags(opts.tags); |
511 | 511 | opts.createSearchChoice = function (term) { return {id: term, text: term}; }; |
512 | | - opts.initSelection = function (element) { |
| 512 | + opts.initSelection = function (element, callback) { |
513 | 513 | var data = []; |
514 | 514 | $(splitVal(element.val(), ",")).each(function () { |
515 | 515 | data.push({id: this, text: this}); |
516 | 516 | }); |
517 | | - return data; |
| 517 | + |
| 518 | + if ($.isFunction(callback)) |
| 519 | + callback(data); |
518 | 520 | }; |
519 | 521 | } |
520 | 522 | } |
|
958 | 960 | var selected; |
959 | 961 | if (this.opts.element.val() === "") { |
960 | 962 | this.updateSelection({id: "", text: ""}); |
| 963 | + this.close(); |
| 964 | + this.setPlaceholder(); |
961 | 965 | } 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 | + }); |
966 | 974 | } |
967 | | - |
968 | | - this.close(); |
969 | | - this.setPlaceholder(); |
970 | 975 | }, |
971 | 976 |
|
972 | 977 | prepareOpts: function () { |
973 | 978 | var opts = this.parent.prepareOpts.apply(this, arguments); |
974 | 979 |
|
975 | 980 | if (opts.element.get(0).tagName.toLowerCase() === "select") { |
976 | 981 | // install sthe selection initializer |
977 | | - opts.initSelection = function (element) { |
| 982 | + opts.initSelection = function (element, callback) { |
978 | 983 | var selected = element.find(":selected"); |
979 | 984 | // 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()}); |
981 | 987 | }; |
982 | 988 | } |
983 | 989 |
|
|
1116 | 1122 |
|
1117 | 1123 | if (opts.element.get(0).tagName.toLowerCase() === "select") { |
1118 | 1124 | // install sthe selection initializer |
1119 | | - opts.initSelection = function (element) { |
| 1125 | + opts.initSelection = function (element,callback) { |
1120 | 1126 | var data = []; |
1121 | 1127 | element.find(":selected").each(function () { |
1122 | 1128 | data.push({id: $(this).attr("value"), text: $(this).text()}); |
1123 | 1129 | }); |
1124 | | - return data; |
| 1130 | + |
| 1131 | + if ($.isFunction(callback)) |
| 1132 | + callback(data); |
1125 | 1133 | }; |
1126 | 1134 | } |
1127 | 1135 |
|
|
1228 | 1236 | var data; |
1229 | 1237 | if (this.opts.element.val() === "") { |
1230 | 1238 | this.updateSelection([]); |
| 1239 | + this.close(); |
| 1240 | + // set the placeholder if necessary |
| 1241 | + this.clearSearch(); |
1231 | 1242 | } |
1232 | 1243 | 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 | + } |
1243 | 1254 | }, |
1244 | 1255 |
|
1245 | 1256 | clearSearch: function () { |
|
0 commit comments