Browse Source

Limit the explicit coupling between adapters

Kevin Brown 10 years ago
parent
commit
94b460f930

+ 31 - 7
dist/js/select2.amd.full.js

@@ -192,6 +192,18 @@ define('select2/data/select',[
     }
   }
 
+  SelectAdapter.prototype.bind = function (container, $container) {
+    var self = this;
+
+    container.on("select", function (params) {
+      var current = self.current(function (data) {
+        //
+      });
+
+      self.select(params.data);
+    });
+  }
+
   SelectAdapter.prototype.query = function (params, callback) {
     var data = [];
     var self = this;
@@ -305,7 +317,7 @@ define('select2/results',[
     return $option;
   }
 
-  Results.prototype.bind = function ($container) {
+  Results.prototype.bind = function (container, $container) {
     var self = this;
 
     this.on("results:all", function (data) {
@@ -390,7 +402,7 @@ define('select2/selection/single',[
     return $selection;
   }
 
-  SingleSelection.prototype.bind = function ($container) {
+  SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -398,6 +410,10 @@ define('select2/selection/single',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    })
   }
 
   SingleSelection.prototype.clear = function () {
@@ -448,7 +464,7 @@ define('select2/selection/multiple',[
     return $selection;
   }
 
-  MultipleSelection.prototype.bind = function ($container) {
+  MultipleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -456,6 +472,10 @@ define('select2/selection/multiple',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    });
   }
 
   MultipleSelection.prototype.clear = function () {
@@ -573,12 +593,15 @@ define('select2/core',[
 
     var self = this;
 
-    this.selection.bind($container);
-    this.results.bind($container);
+    this.data.bind(this, $container);
+    this.selection.bind(this, $container);
+    this.results.bind(this, $container);
 
     this.$element.on("change", function () {
       self.data.current(function (data) {
-        self.selection.update(data);
+        self.trigger("selection:update", {
+          data: data
+        });
       });
     });
 
@@ -587,7 +610,8 @@ define('select2/core',[
     });
 
     this.results.on("selected", function (params) {
-      self.data.select(params.data);
+      self.trigger("select", params);
+
       $container.removeClass("open");
     });
 

+ 31 - 7
dist/js/select2.amd.js

@@ -192,6 +192,18 @@ define('select2/data/select',[
     }
   }
 
+  SelectAdapter.prototype.bind = function (container, $container) {
+    var self = this;
+
+    container.on("select", function (params) {
+      var current = self.current(function (data) {
+        //
+      });
+
+      self.select(params.data);
+    });
+  }
+
   SelectAdapter.prototype.query = function (params, callback) {
     var data = [];
     var self = this;
@@ -305,7 +317,7 @@ define('select2/results',[
     return $option;
   }
 
-  Results.prototype.bind = function ($container) {
+  Results.prototype.bind = function (container, $container) {
     var self = this;
 
     this.on("results:all", function (data) {
@@ -390,7 +402,7 @@ define('select2/selection/single',[
     return $selection;
   }
 
-  SingleSelection.prototype.bind = function ($container) {
+  SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -398,6 +410,10 @@ define('select2/selection/single',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    })
   }
 
   SingleSelection.prototype.clear = function () {
@@ -448,7 +464,7 @@ define('select2/selection/multiple',[
     return $selection;
   }
 
-  MultipleSelection.prototype.bind = function ($container) {
+  MultipleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -456,6 +472,10 @@ define('select2/selection/multiple',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    });
   }
 
   MultipleSelection.prototype.clear = function () {
@@ -573,12 +593,15 @@ define('select2/core',[
 
     var self = this;
 
-    this.selection.bind($container);
-    this.results.bind($container);
+    this.data.bind(this, $container);
+    this.selection.bind(this, $container);
+    this.results.bind(this, $container);
 
     this.$element.on("change", function () {
       self.data.current(function (data) {
-        self.selection.update(data);
+        self.trigger("selection:update", {
+          data: data
+        });
       });
     });
 
@@ -587,7 +610,8 @@ define('select2/core',[
     });
 
     this.results.on("selected", function (params) {
-      self.data.select(params.data);
+      self.trigger("select", params);
+
       $container.removeClass("open");
     });
 

+ 31 - 7
dist/js/select2.full.js

@@ -9729,6 +9729,18 @@ define('select2/data/select',[
     }
   }
 
+  SelectAdapter.prototype.bind = function (container, $container) {
+    var self = this;
+
+    container.on("select", function (params) {
+      var current = self.current(function (data) {
+        //
+      });
+
+      self.select(params.data);
+    });
+  }
+
   SelectAdapter.prototype.query = function (params, callback) {
     var data = [];
     var self = this;
@@ -9842,7 +9854,7 @@ define('select2/results',[
     return $option;
   }
 
-  Results.prototype.bind = function ($container) {
+  Results.prototype.bind = function (container, $container) {
     var self = this;
 
     this.on("results:all", function (data) {
@@ -9927,7 +9939,7 @@ define('select2/selection/single',[
     return $selection;
   }
 
-  SingleSelection.prototype.bind = function ($container) {
+  SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -9935,6 +9947,10 @@ define('select2/selection/single',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    })
   }
 
   SingleSelection.prototype.clear = function () {
@@ -9985,7 +10001,7 @@ define('select2/selection/multiple',[
     return $selection;
   }
 
-  MultipleSelection.prototype.bind = function ($container) {
+  MultipleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -9993,6 +10009,10 @@ define('select2/selection/multiple',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    });
   }
 
   MultipleSelection.prototype.clear = function () {
@@ -10110,12 +10130,15 @@ define('select2/core',[
 
     var self = this;
 
-    this.selection.bind($container);
-    this.results.bind($container);
+    this.data.bind(this, $container);
+    this.selection.bind(this, $container);
+    this.results.bind(this, $container);
 
     this.$element.on("change", function () {
       self.data.current(function (data) {
-        self.selection.update(data);
+        self.trigger("selection:update", {
+          data: data
+        });
       });
     });
 
@@ -10124,7 +10147,8 @@ define('select2/core',[
     });
 
     this.results.on("selected", function (params) {
-      self.data.select(params.data);
+      self.trigger("select", params);
+
       $container.removeClass("open");
     });
 

+ 31 - 7
dist/js/select2.js

@@ -620,6 +620,18 @@ define('select2/data/select',[
     }
   }
 
+  SelectAdapter.prototype.bind = function (container, $container) {
+    var self = this;
+
+    container.on("select", function (params) {
+      var current = self.current(function (data) {
+        //
+      });
+
+      self.select(params.data);
+    });
+  }
+
   SelectAdapter.prototype.query = function (params, callback) {
     var data = [];
     var self = this;
@@ -733,7 +745,7 @@ define('select2/results',[
     return $option;
   }
 
-  Results.prototype.bind = function ($container) {
+  Results.prototype.bind = function (container, $container) {
     var self = this;
 
     this.on("results:all", function (data) {
@@ -818,7 +830,7 @@ define('select2/selection/single',[
     return $selection;
   }
 
-  SingleSelection.prototype.bind = function ($container) {
+  SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -826,6 +838,10 @@ define('select2/selection/single',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    })
   }
 
   SingleSelection.prototype.clear = function () {
@@ -876,7 +892,7 @@ define('select2/selection/multiple',[
     return $selection;
   }
 
-  MultipleSelection.prototype.bind = function ($container) {
+  MultipleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -884,6 +900,10 @@ define('select2/selection/multiple',[
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    });
   }
 
   MultipleSelection.prototype.clear = function () {
@@ -1001,12 +1021,15 @@ define('select2/core',[
 
     var self = this;
 
-    this.selection.bind($container);
-    this.results.bind($container);
+    this.data.bind(this, $container);
+    this.selection.bind(this, $container);
+    this.results.bind(this, $container);
 
     this.$element.on("change", function () {
       self.data.current(function (data) {
-        self.selection.update(data);
+        self.trigger("selection:update", {
+          data: data
+        });
       });
     });
 
@@ -1015,7 +1038,8 @@ define('select2/core',[
     });
 
     this.results.on("selected", function (params) {
-      self.data.select(params.data);
+      self.trigger("select", params);
+
       $container.removeClass("open");
     });
 

+ 2 - 2
playground/basic/decorators.html

@@ -171,10 +171,10 @@ require(["select2/core", "select2/utils", "select2/selection/single",
         return $selection;
     };
 
-    ClearSelection.prototype.bind = function (decorated, $container) {
+    ClearSelection.prototype.bind = function (decorated, container, $container) {
         var self = this;
 
-        decorated.call(this, $container);
+        decorated.call(this, container, $container);
 
         this.$container = $container;
 

+ 8 - 4
src/js/select2/core.js

@@ -49,12 +49,15 @@ define([
 
     var self = this;
 
-    this.selection.bind($container);
-    this.results.bind($container);
+    this.data.bind(this, $container);
+    this.selection.bind(this, $container);
+    this.results.bind(this, $container);
 
     this.$element.on("change", function () {
       self.data.current(function (data) {
-        self.selection.update(data);
+        self.trigger("selection:update", {
+          data: data
+        });
       });
     });
 
@@ -63,7 +66,8 @@ define([
     });
 
     this.results.on("selected", function (params) {
-      self.data.select(params.data);
+      self.trigger("select", params);
+
       $container.removeClass("open");
     });
 

+ 12 - 0
src/js/select2/data/select.js

@@ -54,6 +54,18 @@ define([
     }
   }
 
+  SelectAdapter.prototype.bind = function (container, $container) {
+    var self = this;
+
+    container.on("select", function (params) {
+      var current = self.current(function (data) {
+        //
+      });
+
+      self.select(params.data);
+    });
+  }
+
   SelectAdapter.prototype.query = function (params, callback) {
     var data = [];
     var self = this;

+ 1 - 1
src/js/select2/results.js

@@ -70,7 +70,7 @@ define([
     return $option;
   }
 
-  Results.prototype.bind = function ($container) {
+  Results.prototype.bind = function (container, $container) {
     var self = this;
 
     this.on("results:all", function (data) {

+ 5 - 1
src/js/select2/selection/multiple.js

@@ -22,7 +22,7 @@ define([
     return $selection;
   }
 
-  MultipleSelection.prototype.bind = function ($container) {
+  MultipleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -30,6 +30,10 @@ define([
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    });
   }
 
   MultipleSelection.prototype.clear = function () {

+ 5 - 1
src/js/select2/selection/single.js

@@ -22,7 +22,7 @@ define([
     return $selection;
   }
 
-  SingleSelection.prototype.bind = function ($container) {
+  SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
     this.$selection.on('click', function (evt) {
@@ -30,6 +30,10 @@ define([
         originalEvent: evt
       });
     });
+
+    container.on("selection:update", function (params) {
+      self.update(params.data);
+    })
   }
 
   SingleSelection.prototype.clear = function () {

+ 5 - 11
tests/utils/decorator-tests.js

@@ -2,7 +2,7 @@ module("Decorators")
 
 var Utils = require("select2/utils");
 
-test("overridden", function (assert) {
+test("overridden - method", function (assert) {
     function BaseClass () {};
 
     BaseClass.prototype.hello = function () {
@@ -47,7 +47,7 @@ test("overridden - constructor", function (assert) {
     assert.ok(!inst.inherited);
 });
 
-test("not overridden", function (assert) {
+test("not overridden - method", function (assert) {
     function BaseClass () {};
 
     BaseClass.prototype.hello = function () {
@@ -89,20 +89,14 @@ test("not overridden - constructor", function (assert) {
     assert.ok(inst.called);
 });
 
-test("inherited", function (assert) {
-    function BaseClass () {
-        this.inherited = true;
-    };
+test("inherited - method", function (assert) {
+    function BaseClass () { };
 
     BaseClass.prototype.hello = function () {
         return "A";
     }
 
-    function DecoratorClass (decorated) {
-        this.called = true;
-
-        decorated.call(this);
-    };
+    function DecoratorClass (decorated) { };
 
     DecoratorClass.prototype.hello = function (decorated) {
         return "B" + decorated.call(this) + "C";