|
1302 | 1302 | })); |
1303 | 1303 | }, |
1304 | 1304 |
|
| 1305 | + // single |
1305 | 1306 | clear: function() { |
1306 | 1307 | this.opts.element.val(""); |
1307 | 1308 | this.selection.find("span").empty(); |
|
1438 | 1439 | val = arguments[0]; |
1439 | 1440 |
|
1440 | 1441 | if (this.select) { |
1441 | | - // val is an id |
1442 | 1442 | this.select |
1443 | 1443 | .val(val) |
1444 | 1444 | .find(":selected").each2(function (i, elm) { |
1445 | 1445 | data = {id: elm.attr("value"), text: elm.text()}; |
1446 | 1446 | return false; |
1447 | 1447 | }); |
1448 | 1448 | this.updateSelection(data); |
| 1449 | + this.setPlaceholder(); |
1449 | 1450 | } else { |
1450 | | - // val is an object. !val is true for [undefined,null,''] |
1451 | | - if (this.opts.initSelection && val) { |
1452 | | - that = this; |
1453 | | - this.opts.initSelection(this.opts.element.val(val), function(data){ |
1454 | | - self.opts.element.val(!data ? "" : self.id(data)); |
1455 | | - self.updateSelection(data); |
1456 | | - self.setPlaceholder(); |
1457 | | - }); |
| 1451 | + if (this.opts.initSelection === undefined) { |
| 1452 | + throw new Error("cannot call val() if initSelection() is not defined"); |
| 1453 | + } |
| 1454 | + // val is an id. !val is true for [undefined,null,''] |
| 1455 | + if (!val) { |
| 1456 | + this.clear(); |
1458 | 1457 | return; |
1459 | | - } else { |
1460 | | - this.opts.element.val(!val ? "" : this.id(val)); |
1461 | | - this.updateSelection(val); |
1462 | 1458 | } |
| 1459 | + this.opts.initSelection(this.opts.element, function(data){ |
| 1460 | + self.opts.element.val(!data ? "" : self.id(data)); |
| 1461 | + self.updateSelection(data); |
| 1462 | + self.setPlaceholder(); |
| 1463 | + }); |
1463 | 1464 | } |
1464 | | - this.setPlaceholder(); |
1465 | | - |
1466 | 1465 | }, |
1467 | 1466 |
|
1468 | 1467 | // single |
|
1907 | 1906 |
|
1908 | 1907 | val = arguments[0]; |
1909 | 1908 |
|
| 1909 | + if (!val) { |
| 1910 | + this.opts.element.val(""); |
| 1911 | + this.updateSelection([]); |
| 1912 | + this.clearSearch(); |
| 1913 | + return; |
| 1914 | + } |
| 1915 | + |
| 1916 | + // val is a list of ids |
| 1917 | + this.setVal(val); |
| 1918 | + |
1910 | 1919 | if (this.select) { |
1911 | | - // val is a list of ids |
1912 | | - this.setVal(val); |
1913 | 1920 | this.select.find(":selected").each(function () { |
1914 | 1921 | data.push({id: $(this).attr("value"), text: $(this).text()}); |
1915 | 1922 | }); |
1916 | 1923 | this.updateSelection(data); |
1917 | 1924 | } else { |
1918 | | - if (this.opts.initSelection && val !== null) { |
1919 | | - this.opts.initSelection(this.opts.element.val(val), function(newVal){ |
1920 | | - $(newVal).each(function () { data.push(self.id(this)); }); |
1921 | | - self.setVal(data); |
1922 | | - self.updateSelection(newVal); |
1923 | | - self.clearSearch(); |
1924 | | - }); |
1925 | | - return; |
1926 | | - } else { |
1927 | | - val = (val === null) ? [] : val; |
1928 | | - // val is a list of objects |
1929 | | - $(val).each(function () { data.push(self.id(this)); }); |
1930 | | - this.setVal(data); |
1931 | | - this.updateSelection(val); |
| 1925 | + if (this.opts.initSelection === undefined) { |
| 1926 | + throw new Error("val() cannot be called if initSelection() is not defined") |
1932 | 1927 | } |
1933 | | - } |
1934 | 1928 |
|
| 1929 | + this.opts.initSelection(this.opts.element, function(data){ |
| 1930 | + var ids=$(data).map(self.id); |
| 1931 | + self.setVal(ids); |
| 1932 | + self.updateSelection(data); |
| 1933 | + self.clearSearch(); |
| 1934 | + }); |
| 1935 | + } |
1935 | 1936 | this.clearSearch(); |
1936 | 1937 | }, |
1937 | 1938 |
|
|
0 commit comments