From 544b22718a84c8e3201d01e128ecacf707c4e03e Mon Sep 17 00:00:00 2001
From: Laurens Meurs <lmeurs@1139006.no-reply.drupal.org>
Date: Thu, 30 Apr 2015 13:24:28 +0200
Subject: [PATCH] Issue #2226405 by Heine, lmeurs: Fixes bug with
 initialization of state dependent elements

---
 misc/states.js | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/misc/states.js b/misc/states.js
index 6d98da8..5a9b1c6 100644
--- a/misc/states.js
+++ b/misc/states.js
@@ -305,12 +305,7 @@ states.Trigger = function (args) {
 
   if (this.state in states.Trigger.states) {
     this.element = $(this.selector);
-
-    // Only call the trigger initializer when it wasn't yet attached to this
-    // element. Otherwise we'd end up with duplicate events.
-    if (!this.element.data('trigger:' + this.state)) {
-      this.initialize();
-    }
+    this.initialize();
   }
 };
 
@@ -329,16 +324,14 @@ states.Trigger.prototype = {
         }
       }
     }
-
-    // Mark this trigger as initialized for this element.
-    this.element.data('trigger:' + this.state, true);
   },
 
   defaultTrigger: function (event, valueFn) {
     var oldValue = valueFn.call(this.element);
 
-    // Attach the event callback.
-    this.element.bind(event, $.proxy(function (e) {
+    // Detach possibly earlier attached event callback using event namespacing
+    // and attach a new event callback.
+    this.element.unbind(event + '.statesDefaultTrigger').bind(event + '.statesDefaultTrigger', $.proxy(function (e) {
       var value = valueFn.call(this.element, e);
       // Only trigger the event if the value has actually changed.
       if (oldValue !== value) {
-- 
1.9.0.msysgit.0

