浏览代码

Small changes in preparation for more adapters

Kevin Brown 10 年之前
父节点
当前提交
3e05e32eda

+ 21 - 8
dist/js/select2.amd.full.js

@@ -245,10 +245,17 @@ define('select2/data/select',[
   };
 
   SelectAdapter.prototype.item = function ($option) {
-    var data = {
-      id: $option.val(),
-      text: $option.html()
-    };
+    var data = $option.data("data");
+
+    // If the data has already be generated, use it
+    if (data == null) {
+      data = {
+        id: $option.val(),
+        text: $option.html()
+      };
+
+      $option.data("data", data);
+    }
 
     return data;
   };
@@ -356,7 +363,7 @@ define('select2/results',[
       self.setClasses();
     })
 
-    this.$results.on("click", ".option", function (evt) {
+    this.$results.on("mouseup", ".option", function (evt) {
       var $this = $(this);
 
       var data = $this.data("data");
@@ -441,7 +448,7 @@ define('select2/selection/single',[
   SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
-    this.$selection.on('click', function (evt) {
+    this.$selection.on('mousedown', function (evt) {
       self.trigger("toggle", {
         originalEvent: evt
       });
@@ -561,7 +568,7 @@ define('select2/options',[
   function Options (options) {
     this.options = options;
 
-    this.dataAdapter = SelectData;
+    this.dataAdapter = options.dataAdapter || SelectData;
     this.resultsAdapter = ResultsList;
     this.dropdownAdapter = options.dropdownAdapter || Dropdown;
     this.selectionAdapter = options.selectionAdapter;
@@ -660,12 +667,18 @@ define('select2/core',[
     // Set the initial state
 
     this.data.current(function (initialData) {
-      self.selection.update(initialData);
+      self.trigger("selection:update", {
+        data: initialData
+      });
     });
 
     this.data.query({}, function (data) {
       self.results.trigger("results:all", data);
     });
+
+    // Hide the original select
+
+    $element.hide();
   };
 
   Utils.Extend(Select2, Utils.Observable);

+ 21 - 8
dist/js/select2.amd.js

@@ -245,10 +245,17 @@ define('select2/data/select',[
   };
 
   SelectAdapter.prototype.item = function ($option) {
-    var data = {
-      id: $option.val(),
-      text: $option.html()
-    };
+    var data = $option.data("data");
+
+    // If the data has already be generated, use it
+    if (data == null) {
+      data = {
+        id: $option.val(),
+        text: $option.html()
+      };
+
+      $option.data("data", data);
+    }
 
     return data;
   };
@@ -356,7 +363,7 @@ define('select2/results',[
       self.setClasses();
     })
 
-    this.$results.on("click", ".option", function (evt) {
+    this.$results.on("mouseup", ".option", function (evt) {
       var $this = $(this);
 
       var data = $this.data("data");
@@ -441,7 +448,7 @@ define('select2/selection/single',[
   SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
-    this.$selection.on('click', function (evt) {
+    this.$selection.on('mousedown', function (evt) {
       self.trigger("toggle", {
         originalEvent: evt
       });
@@ -561,7 +568,7 @@ define('select2/options',[
   function Options (options) {
     this.options = options;
 
-    this.dataAdapter = SelectData;
+    this.dataAdapter = options.dataAdapter || SelectData;
     this.resultsAdapter = ResultsList;
     this.dropdownAdapter = options.dropdownAdapter || Dropdown;
     this.selectionAdapter = options.selectionAdapter;
@@ -660,12 +667,18 @@ define('select2/core',[
     // Set the initial state
 
     this.data.current(function (initialData) {
-      self.selection.update(initialData);
+      self.trigger("selection:update", {
+        data: initialData
+      });
     });
 
     this.data.query({}, function (data) {
       self.results.trigger("results:all", data);
     });
+
+    // Hide the original select
+
+    $element.hide();
   };
 
   Utils.Extend(Select2, Utils.Observable);

+ 21 - 8
dist/js/select2.full.js

@@ -9782,10 +9782,17 @@ define('select2/data/select',[
   };
 
   SelectAdapter.prototype.item = function ($option) {
-    var data = {
-      id: $option.val(),
-      text: $option.html()
-    };
+    var data = $option.data("data");
+
+    // If the data has already be generated, use it
+    if (data == null) {
+      data = {
+        id: $option.val(),
+        text: $option.html()
+      };
+
+      $option.data("data", data);
+    }
 
     return data;
   };
@@ -9893,7 +9900,7 @@ define('select2/results',[
       self.setClasses();
     })
 
-    this.$results.on("click", ".option", function (evt) {
+    this.$results.on("mouseup", ".option", function (evt) {
       var $this = $(this);
 
       var data = $this.data("data");
@@ -9978,7 +9985,7 @@ define('select2/selection/single',[
   SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
-    this.$selection.on('click', function (evt) {
+    this.$selection.on('mousedown', function (evt) {
       self.trigger("toggle", {
         originalEvent: evt
       });
@@ -10098,7 +10105,7 @@ define('select2/options',[
   function Options (options) {
     this.options = options;
 
-    this.dataAdapter = SelectData;
+    this.dataAdapter = options.dataAdapter || SelectData;
     this.resultsAdapter = ResultsList;
     this.dropdownAdapter = options.dropdownAdapter || Dropdown;
     this.selectionAdapter = options.selectionAdapter;
@@ -10197,12 +10204,18 @@ define('select2/core',[
     // Set the initial state
 
     this.data.current(function (initialData) {
-      self.selection.update(initialData);
+      self.trigger("selection:update", {
+        data: initialData
+      });
     });
 
     this.data.query({}, function (data) {
       self.results.trigger("results:all", data);
     });
+
+    // Hide the original select
+
+    $element.hide();
   };
 
   Utils.Extend(Select2, Utils.Observable);

+ 21 - 8
dist/js/select2.js

@@ -673,10 +673,17 @@ define('select2/data/select',[
   };
 
   SelectAdapter.prototype.item = function ($option) {
-    var data = {
-      id: $option.val(),
-      text: $option.html()
-    };
+    var data = $option.data("data");
+
+    // If the data has already be generated, use it
+    if (data == null) {
+      data = {
+        id: $option.val(),
+        text: $option.html()
+      };
+
+      $option.data("data", data);
+    }
 
     return data;
   };
@@ -784,7 +791,7 @@ define('select2/results',[
       self.setClasses();
     })
 
-    this.$results.on("click", ".option", function (evt) {
+    this.$results.on("mouseup", ".option", function (evt) {
       var $this = $(this);
 
       var data = $this.data("data");
@@ -869,7 +876,7 @@ define('select2/selection/single',[
   SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
-    this.$selection.on('click', function (evt) {
+    this.$selection.on('mousedown', function (evt) {
       self.trigger("toggle", {
         originalEvent: evt
       });
@@ -989,7 +996,7 @@ define('select2/options',[
   function Options (options) {
     this.options = options;
 
-    this.dataAdapter = SelectData;
+    this.dataAdapter = options.dataAdapter || SelectData;
     this.resultsAdapter = ResultsList;
     this.dropdownAdapter = options.dropdownAdapter || Dropdown;
     this.selectionAdapter = options.selectionAdapter;
@@ -1088,12 +1095,18 @@ define('select2/core',[
     // Set the initial state
 
     this.data.current(function (initialData) {
-      self.selection.update(initialData);
+      self.trigger("selection:update", {
+        data: initialData
+      });
     });
 
     this.data.query({}, function (data) {
       self.results.trigger("results:all", data);
     });
+
+    // Hide the original select
+
+    $element.hide();
   };
 
   Utils.Extend(Select2, Utils.Observable);

+ 7 - 1
src/js/select2/core.js

@@ -80,12 +80,18 @@ define([
     // Set the initial state
 
     this.data.current(function (initialData) {
-      self.selection.update(initialData);
+      self.trigger("selection:update", {
+        data: initialData
+      });
     });
 
     this.data.query({}, function (data) {
       self.results.trigger("results:all", data);
     });
+
+    // Hide the original select
+
+    $element.hide();
   };
 
   Utils.Extend(Select2, Utils.Observable);

+ 11 - 4
src/js/select2/data/select.js

@@ -107,10 +107,17 @@ define([
   };
 
   SelectAdapter.prototype.item = function ($option) {
-    var data = {
-      id: $option.val(),
-      text: $option.html()
-    };
+    var data = $option.data("data");
+
+    // If the data has already be generated, use it
+    if (data == null) {
+      data = {
+        id: $option.val(),
+        text: $option.html()
+      };
+
+      $option.data("data", data);
+    }
 
     return data;
   };

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

@@ -9,7 +9,7 @@ define([
   function Options (options) {
     this.options = options;
 
-    this.dataAdapter = SelectData;
+    this.dataAdapter = options.dataAdapter || SelectData;
     this.resultsAdapter = ResultsList;
     this.dropdownAdapter = options.dropdownAdapter || Dropdown;
     this.selectionAdapter = options.selectionAdapter;

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

@@ -86,7 +86,7 @@ define([
       self.setClasses();
     })
 
-    this.$results.on("click", ".option", function (evt) {
+    this.$results.on("mouseup", ".option", function (evt) {
       var $this = $(this);
 
       var data = $this.data("data");

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

@@ -25,7 +25,7 @@ define([
   SingleSelection.prototype.bind = function (container, $container) {
     var self = this;
 
-    this.$selection.on('click', function (evt) {
+    this.$selection.on('mousedown', function (evt) {
       self.trigger("toggle", {
         originalEvent: evt
       });