Skip to content

Commit 1165289

Browse files
committed
clean up val. now it only accepts ids and only when initselection is set
1 parent ad8fd25 commit 1165289

1 file changed

Lines changed: 32 additions & 31 deletions

File tree

select2.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@
13021302
}));
13031303
},
13041304

1305+
// single
13051306
clear: function() {
13061307
this.opts.element.val("");
13071308
this.selection.find("span").empty();
@@ -1438,31 +1439,29 @@
14381439
val = arguments[0];
14391440

14401441
if (this.select) {
1441-
// val is an id
14421442
this.select
14431443
.val(val)
14441444
.find(":selected").each2(function (i, elm) {
14451445
data = {id: elm.attr("value"), text: elm.text()};
14461446
return false;
14471447
});
14481448
this.updateSelection(data);
1449+
this.setPlaceholder();
14491450
} 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();
14581457
return;
1459-
} else {
1460-
this.opts.element.val(!val ? "" : this.id(val));
1461-
this.updateSelection(val);
14621458
}
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+
});
14631464
}
1464-
this.setPlaceholder();
1465-
14661465
},
14671466

14681467
// single
@@ -1907,31 +1906,33 @@
19071906

19081907
val = arguments[0];
19091908

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+
19101919
if (this.select) {
1911-
// val is a list of ids
1912-
this.setVal(val);
19131920
this.select.find(":selected").each(function () {
19141921
data.push({id: $(this).attr("value"), text: $(this).text()});
19151922
});
19161923
this.updateSelection(data);
19171924
} 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")
19321927
}
1933-
}
19341928

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+
}
19351936
this.clearSearch();
19361937
},
19371938

0 commit comments

Comments
 (0)