Skip to content

Commit

Permalink
Broke out a base data adapter
Browse files Browse the repository at this point in the history
This should allow us to create a basic interface that all adapters
must follow.
  • Loading branch information
kevin-brown committed Oct 22, 2014
1 parent 08ac13d commit 114732e
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 10 deletions.
25 changes: 23 additions & 2 deletions dist/js/select2.amd.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
25 changes: 23 additions & 2 deletions dist/js/select2.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
25 changes: 23 additions & 2 deletions dist/js/select2.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
25 changes: 23 additions & 2 deletions dist/js/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
19 changes: 19 additions & 0 deletions src/js/select2/data/base.js
Original file line number Diff line number Diff line change
@@ -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;
});
5 changes: 3 additions & 2 deletions src/js/select2/data/select.js
Original file line number Diff line number Diff line change
@@ -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 = [];
Expand Down
29 changes: 29 additions & 0 deletions tests/data/base-tests.js
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions tests/data/base.html
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 114732e

Please sign in to comment.