|
659 | 659 | } else if ("tags" in opts) { |
660 | 660 | opts.query = tags(opts.tags); |
661 | 661 | opts.createSearchChoice = function (term) { return {id: term, text: term}; }; |
662 | | - opts.initSelection = function (element) { |
| 662 | + opts.initSelection = function (element, callback) { |
663 | 663 | var data = []; |
664 | 664 | $(splitVal(element.val(), ",")).each(function () { |
665 | 665 | data.push({id: this, text: this}); |
666 | 666 | }); |
667 | | - return data; |
| 667 | + |
| 668 | + if ($.isFunction(callback)) |
| 669 | + callback(data); |
668 | 670 | }; |
669 | 671 | } |
670 | 672 | } |
|
1174 | 1176 | var selected; |
1175 | 1177 | if (this.opts.element.val() === "") { |
1176 | 1178 | this.updateSelection({id: "", text: ""}); |
| 1179 | + this.close(); |
| 1180 | + this.setPlaceholder(); |
1177 | 1181 | } 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 | + }); |
1182 | 1190 | } |
1183 | | - |
1184 | | - this.close(); |
1185 | | - this.setPlaceholder(); |
1186 | 1191 | }, |
1187 | 1192 |
|
1188 | 1193 | // single |
|
1191 | 1196 |
|
1192 | 1197 | if (opts.element.get(0).tagName.toLowerCase() === "select") { |
1193 | 1198 | // install sthe selection initializer |
1194 | | - opts.initSelection = function (element) { |
| 1199 | + opts.initSelection = function (element, callback) { |
1195 | 1200 | var selected = element.find(":selected"); |
1196 | 1201 | // 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()}); |
1198 | 1204 | }; |
1199 | 1205 | } |
1200 | 1206 |
|
|
1342 | 1348 | // TODO validate placeholder is a string if specified |
1343 | 1349 |
|
1344 | 1350 | 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 | + |
1347 | 1354 | var data = []; |
1348 | 1355 | element.find(":selected").each2(function (i, elm) { |
1349 | 1356 | data.push({id: elm.attr("value"), text: elm.text()}); |
1350 | 1357 | }); |
1351 | | - return data; |
| 1358 | + |
| 1359 | + if ($.isFunction(callback)) |
| 1360 | + callback(data); |
1352 | 1361 | }; |
1353 | 1362 | } |
1354 | 1363 |
|
|
1460 | 1469 | var data; |
1461 | 1470 | if (this.opts.element.val() === "") { |
1462 | 1471 | this.updateSelection([]); |
| 1472 | + this.close(); |
| 1473 | + // set the placeholder if necessary |
| 1474 | + this.clearSearch(); |
1463 | 1475 | } |
1464 | 1476 | 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 | + } |
1475 | 1487 | }, |
1476 | 1488 |
|
1477 | 1489 | // multi |
|
0 commit comments