Browse Source

Broke out a base data adapter

This should allow us to create a basic interface that all adapters
must follow.
Kevin Brown 10 years ago
parent
commit
114732ec25

+ 23 - 2
dist/js/select2.amd.full.js

@@ -136,17 +136,38 @@ define('select2/utils',[], function () {
   return Utils;
 });
 
+define('select2/data/base',[
+  '../utils'
+], function (Utils) {
+  function BaseAdapter ($element, options) {
+    BaseAdapter.__super__.constructor.call(this);
+  }
+
+  Utils.Extend(BaseAdapter, Utils.Observable);
+
+  BaseAdapter.prototype.current = function (callback) {
+    throw new Error("The `current` method must be defined in child classes.");
+  }
+
+  BaseAdapter.prototype.query = function (params, callback) {
+    throw new Error("The `query` method must be defined in child classes.");
+  }
+
+  return BaseAdapter;
+});
+
 define('select2/data/select',[
+  './base',
   '../utils',
   'jquery'
-], function (Utils, $) {
+], function (BaseAdapter, Utils, $) {
   function SelectAdapter ($element, options) {
     this.$element = $element;
 
     SelectAdapter.__super__.constructor.call(this);
   }
 
-  Utils.Extend(SelectAdapter, Utils.Observable);
+  Utils.Extend(SelectAdapter, BaseAdapter);
 
   SelectAdapter.prototype.current = function (callback) {
     var data = [];

+ 23 - 2
dist/js/select2.amd.js

@@ -136,17 +136,38 @@ define('select2/utils',[], function () {
   return Utils;
 });
 
+define('select2/data/base',[
+  '../utils'
+], function (Utils) {
+  function BaseAdapter ($element, options) {
+    BaseAdapter.__super__.constructor.call(this);
+  }
+
+  Utils.Extend(BaseAdapter, Utils.Observable);
+
+  BaseAdapter.prototype.current = function (callback) {
+    throw new Error("The `current` method must be defined in child classes.");
+  }
+
+  BaseAdapter.prototype.query = function (params, callback) {
+    throw new Error("The `query` method must be defined in child classes.");
+  }
+
+  return BaseAdapter;
+});
+
 define('select2/data/select',[
+  './base',
   '../utils',
   'jquery'
-], function (Utils, $) {
+], function (BaseAdapter, Utils, $) {
   function SelectAdapter ($element, options) {
     this.$element = $element;
 
     SelectAdapter.__super__.constructor.call(this);
   }
 
-  Utils.Extend(SelectAdapter, Utils.Observable);
+  Utils.Extend(SelectAdapter, BaseAdapter);
 
   SelectAdapter.prototype.current = function (callback) {
     var data = [];

+ 23 - 2
dist/js/select2.full.js

@@ -9673,17 +9673,38 @@ define('select2/utils',[], function () {
   return Utils;
 });
 
+define('select2/data/base',[
+  '../utils'
+], function (Utils) {
+  function BaseAdapter ($element, options) {
+    BaseAdapter.__super__.constructor.call(this);
+  }
+
+  Utils.Extend(BaseAdapter, Utils.Observable);
+
+  BaseAdapter.prototype.current = function (callback) {
+    throw new Error("The `current` method must be defined in child classes.");
+  }
+
+  BaseAdapter.prototype.query = function (params, callback) {
+    throw new Error("The `query` method must be defined in child classes.");
+  }
+
+  return BaseAdapter;
+});
+
 define('select2/data/select',[
+  './base',
   '../utils',
   'jquery'
-], function (Utils, $) {
+], function (BaseAdapter, Utils, $) {
   function SelectAdapter ($element, options) {
     this.$element = $element;
 
     SelectAdapter.__super__.constructor.call(this);
   }
 
-  Utils.Extend(SelectAdapter, Utils.Observable);
+  Utils.Extend(SelectAdapter, BaseAdapter);
 
   SelectAdapter.prototype.current = function (callback) {
     var data = [];

+ 23 - 2
dist/js/select2.js

@@ -564,17 +564,38 @@ define('select2/utils',[], function () {
   return Utils;
 });
 
+define('select2/data/base',[
+  '../utils'
+], function (Utils) {
+  function BaseAdapter ($element, options) {
+    BaseAdapter.__super__.constructor.call(this);
+  }
+
+  Utils.Extend(BaseAdapter, Utils.Observable);
+
+  BaseAdapter.prototype.current = function (callback) {
+    throw new Error("The `current` method must be defined in child classes.");
+  }
+
+  BaseAdapter.prototype.query = function (params, callback) {
+    throw new Error("The `query` method must be defined in child classes.");
+  }
+
+  return BaseAdapter;
+});
+
 define('select2/data/select',[
+  './base',
   '../utils',
   'jquery'
-], function (Utils, $) {
+], function (BaseAdapter, Utils, $) {
   function SelectAdapter ($element, options) {
     this.$element = $element;
 
     SelectAdapter.__super__.constructor.call(this);
   }
 
-  Utils.Extend(SelectAdapter, Utils.Observable);
+  Utils.Extend(SelectAdapter, BaseAdapter);
 
   SelectAdapter.prototype.current = function (callback) {
     var data = [];

+ 19 - 0
src/js/select2/data/base.js

@@ -0,0 +1,19 @@
+define([
+  '../utils'
+], function (Utils) {
+  function BaseAdapter ($element, options) {
+    BaseAdapter.__super__.constructor.call(this);
+  }
+
+  Utils.Extend(BaseAdapter, Utils.Observable);
+
+  BaseAdapter.prototype.current = function (callback) {
+    throw new Error("The `current` method must be defined in child classes.");
+  }
+
+  BaseAdapter.prototype.query = function (params, callback) {
+    throw new Error("The `query` method must be defined in child classes.");
+  }
+
+  return BaseAdapter;
+});

+ 3 - 2
src/js/select2/data/select.js

@@ -1,14 +1,15 @@
 define([
+  './base',
   '../utils',
   'jquery'
-], function (Utils, $) {
+], function (BaseAdapter, Utils, $) {
   function SelectAdapter ($element, options) {
     this.$element = $element;
 
     SelectAdapter.__super__.constructor.call(this);
   }
 
-  Utils.Extend(SelectAdapter, Utils.Observable);
+  Utils.Extend(SelectAdapter, BaseAdapter);
 
   SelectAdapter.prototype.current = function (callback) {
     var data = [];

+ 29 - 0
tests/data/base-tests.js

@@ -0,0 +1,29 @@
+module("Data adapters - Base")
+
+var BaseData = require("select2/data/base");
+var $ = require("jquery");
+var Options = require("select2/options");
+
+var options = new Options({});
+
+test("current is required", function (assert) {
+  var data = new BaseData($("#qunit-fixture select"), options);
+
+  assert.throws(
+    function () {
+      data.current(function () {});
+    },
+    "current has no default implementation"
+  )
+});
+
+test("query is required", function (assert) {
+  var data = new BaseData($("#qunit-fixture select"), options);
+
+  assert.throws(
+    function () {
+      data.query({}, function () {});
+    },
+    "query has no default implementation"
+  );
+});

+ 20 - 0
tests/data/base.html

@@ -0,0 +1,20 @@
+<!doctype html>
+<html>
+  <head>
+    <link rel="stylesheet" href="../vendor/qunit-1.14.0.css" type="text/css" />
+    <link rel="stylesheet" href="../../dist/css/select2.css" type="text/css" />
+  </head>
+  <body>
+    <div id="qunit"></div>
+    <div id="qunit-fixture">
+      <select></select>
+    </div>
+
+    <script src="../vendor/qunit-1.14.0.js" type="text/javascript"></script>
+    <script src="../../vendor/almond-0.2.9.js" type="text/javascript"></script>
+    <script src="../../vendor/jquery-2.1.0.js" type="text/javascript"></script>
+    <script src="../../dist/js/select2.amd.js" type="text/javascript"></script>
+
+    <script src="base-tests.js" type="text/javascript"></script>
+  </body>
+</html>