Browse Source

Triggers change when value is set programaticaly.

``` js
$('#select').select2().
    on('change', function () {
        alert('change');
    }).
    val(['foo']);
```

Now shows an alert window.
Julien Muetton 12 years ago
parent
commit
09d29585a0
1 changed files with 6 additions and 0 deletions
  1. 6 0
      select2.js

+ 6 - 0
select2.js

@@ -1739,6 +1739,7 @@ the specific language governing permissions and limitations under the Apache Lic
                     });
                     });
                 this.updateSelection(data);
                 this.updateSelection(data);
                 this.setPlaceholder();
                 this.setPlaceholder();
+                this.triggerChange();
             } else {
             } else {
                 if (this.opts.initSelection === undefined) {
                 if (this.opts.initSelection === undefined) {
                     throw new Error("cannot call val() if initSelection() is not defined");
                     throw new Error("cannot call val() if initSelection() is not defined");
@@ -1746,6 +1747,7 @@ the specific language governing permissions and limitations under the Apache Lic
                 // val is an id. !val is true for [undefined,null,'']
                 // val is an id. !val is true for [undefined,null,'']
                 if (!val) {
                 if (!val) {
                     this.clear();
                     this.clear();
+                    this.triggerChange();
                     return;
                     return;
                 }
                 }
                 this.opts.element.val(val);
                 this.opts.element.val(val);
@@ -1753,6 +1755,7 @@ the specific language governing permissions and limitations under the Apache Lic
                     self.opts.element.val(!data ? "" : self.id(data));
                     self.opts.element.val(!data ? "" : self.id(data));
                     self.updateSelection(data);
                     self.updateSelection(data);
                     self.setPlaceholder();
                     self.setPlaceholder();
+                    self.triggerChange();
                 });
                 });
             }
             }
         },
         },
@@ -2251,6 +2254,7 @@ the specific language governing permissions and limitations under the Apache Lic
                 this.opts.element.val("");
                 this.opts.element.val("");
                 this.updateSelection([]);
                 this.updateSelection([]);
                 this.clearSearch();
                 this.clearSearch();
+                this.triggerChange();
                 return;
                 return;
             }
             }
 
 
@@ -2262,6 +2266,7 @@ the specific language governing permissions and limitations under the Apache Lic
                     data.push({id: $(this).attr("value"), text: $(this).text()});
                     data.push({id: $(this).attr("value"), text: $(this).text()});
                 });
                 });
                 this.updateSelection(data);
                 this.updateSelection(data);
+                this.triggerChange();
             } else {
             } else {
                 if (this.opts.initSelection === undefined) {
                 if (this.opts.initSelection === undefined) {
                     throw new Error("val() cannot be called if initSelection() is not defined")
                     throw new Error("val() cannot be called if initSelection() is not defined")
@@ -2272,6 +2277,7 @@ the specific language governing permissions and limitations under the Apache Lic
                     self.setVal(ids);
                     self.setVal(ids);
                     self.updateSelection(data);
                     self.updateSelection(data);
                     self.clearSearch();
                     self.clearSearch();
+                    self.triggerChange();
                 });
                 });
             }
             }
             this.clearSearch();
             this.clearSearch();