Selaa lähdekoodia

add a rejectable select event. fixes #1053

Igor Vaynberg 12 vuotta sitten
vanhempi
commit
dc27a5b74d
1 muutettua tiedostoa jossa 13 lisäystä ja 0 poistoa
  1. 13 0
      select2.js

+ 13 - 0
select2.js

@@ -964,6 +964,13 @@ the specific language governing permissions and limitations under the Apache Lic
             }
         },
 
+        // abstract
+        triggerSelect: function(data) {
+            var evt = $.Event("selected", { val: this.id(data), object: data });
+            this.opts.element.trigger(evt);
+            return !evt.isDefaultPrevented();
+        },
+
         /**
          * Triggers the change event on the source element
          */
@@ -1974,6 +1981,9 @@ the specific language governing permissions and limitations under the Apache Lic
 
         // single
         onSelect: function (data, options) {
+
+            if (!this.triggerSelect(data)) { return; }
+
             var old = this.opts.element.val(),
                 oldData = this.data();
 
@@ -2407,6 +2417,9 @@ the specific language governing permissions and limitations under the Apache Lic
 
         // multi
         onSelect: function (data, options) {
+
+            if (!this.triggerSelect(data)) { return; }
+
             this.addSelectedChoice(data);
 
             this.opts.element.trigger({ type: "selected", val: this.id(data), choice: data });