Skip to content

Commit 114732e

Browse files
committed
Broke out a base data adapter
This should allow us to create a basic interface that all adapters must follow.
1 parent 08ac13d commit 114732e

8 files changed

Lines changed: 163 additions & 10 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,38 @@ define('select2/utils',[], function () {
136136
return Utils;
137137
});
138138

139+
define('select2/data/base',[
140+
'../utils'
141+
], function (Utils) {
142+
function BaseAdapter ($element, options) {
143+
BaseAdapter.__super__.constructor.call(this);
144+
}
145+
146+
Utils.Extend(BaseAdapter, Utils.Observable);
147+
148+
BaseAdapter.prototype.current = function (callback) {
149+
throw new Error("The `current` method must be defined in child classes.");
150+
}
151+
152+
BaseAdapter.prototype.query = function (params, callback) {
153+
throw new Error("The `query` method must be defined in child classes.");
154+
}
155+
156+
return BaseAdapter;
157+
});
158+
139159
define('select2/data/select',[
160+
'./base',
140161
'../utils',
141162
'jquery'
142-
], function (Utils, $) {
163+
], function (BaseAdapter, Utils, $) {
143164
function SelectAdapter ($element, options) {
144165
this.$element = $element;
145166

146167
SelectAdapter.__super__.constructor.call(this);
147168
}
148169

149-
Utils.Extend(SelectAdapter, Utils.Observable);
170+
Utils.Extend(SelectAdapter, BaseAdapter);
150171

151172
SelectAdapter.prototype.current = function (callback) {
152173
var data = [];

dist/js/select2.amd.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,38 @@ define('select2/utils',[], function () {
136136
return Utils;
137137
});
138138

139+
define('select2/data/base',[
140+
'../utils'
141+
], function (Utils) {
142+
function BaseAdapter ($element, options) {
143+
BaseAdapter.__super__.constructor.call(this);
144+
}
145+
146+
Utils.Extend(BaseAdapter, Utils.Observable);
147+
148+
BaseAdapter.prototype.current = function (callback) {
149+
throw new Error("The `current` method must be defined in child classes.");
150+
}
151+
152+
BaseAdapter.prototype.query = function (params, callback) {
153+
throw new Error("The `query` method must be defined in child classes.");
154+
}
155+
156+
return BaseAdapter;
157+
});
158+
139159
define('select2/data/select',[
160+
'./base',
140161
'../utils',
141162
'jquery'
142-
], function (Utils, $) {
163+
], function (BaseAdapter, Utils, $) {
143164
function SelectAdapter ($element, options) {
144165
this.$element = $element;
145166

146167
SelectAdapter.__super__.constructor.call(this);
147168
}
148169

149-
Utils.Extend(SelectAdapter, Utils.Observable);
170+
Utils.Extend(SelectAdapter, BaseAdapter);
150171

151172
SelectAdapter.prototype.current = function (callback) {
152173
var data = [];

dist/js/select2.full.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9673,17 +9673,38 @@ define('select2/utils',[], function () {
96739673
return Utils;
96749674
});
96759675

9676+
define('select2/data/base',[
9677+
'../utils'
9678+
], function (Utils) {
9679+
function BaseAdapter ($element, options) {
9680+
BaseAdapter.__super__.constructor.call(this);
9681+
}
9682+
9683+
Utils.Extend(BaseAdapter, Utils.Observable);
9684+
9685+
BaseAdapter.prototype.current = function (callback) {
9686+
throw new Error("The `current` method must be defined in child classes.");
9687+
}
9688+
9689+
BaseAdapter.prototype.query = function (params, callback) {
9690+
throw new Error("The `query` method must be defined in child classes.");
9691+
}
9692+
9693+
return BaseAdapter;
9694+
});
9695+
96769696
define('select2/data/select',[
9697+
'./base',
96779698
'../utils',
96789699
'jquery'
9679-
], function (Utils, $) {
9700+
], function (BaseAdapter, Utils, $) {
96809701
function SelectAdapter ($element, options) {
96819702
this.$element = $element;
96829703

96839704
SelectAdapter.__super__.constructor.call(this);
96849705
}
96859706

9686-
Utils.Extend(SelectAdapter, Utils.Observable);
9707+
Utils.Extend(SelectAdapter, BaseAdapter);
96879708

96889709
SelectAdapter.prototype.current = function (callback) {
96899710
var data = [];

dist/js/select2.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,17 +564,38 @@ define('select2/utils',[], function () {
564564
return Utils;
565565
});
566566

567+
define('select2/data/base',[
568+
'../utils'
569+
], function (Utils) {
570+
function BaseAdapter ($element, options) {
571+
BaseAdapter.__super__.constructor.call(this);
572+
}
573+
574+
Utils.Extend(BaseAdapter, Utils.Observable);
575+
576+
BaseAdapter.prototype.current = function (callback) {
577+
throw new Error("The `current` method must be defined in child classes.");
578+
}
579+
580+
BaseAdapter.prototype.query = function (params, callback) {
581+
throw new Error("The `query` method must be defined in child classes.");
582+
}
583+
584+
return BaseAdapter;
585+
});
586+
567587
define('select2/data/select',[
588+
'./base',
568589
'../utils',
569590
'jquery'
570-
], function (Utils, $) {
591+
], function (BaseAdapter, Utils, $) {
571592
function SelectAdapter ($element, options) {
572593
this.$element = $element;
573594

574595
SelectAdapter.__super__.constructor.call(this);
575596
}
576597

577-
Utils.Extend(SelectAdapter, Utils.Observable);
598+
Utils.Extend(SelectAdapter, BaseAdapter);
578599

579600
SelectAdapter.prototype.current = function (callback) {
580601
var data = [];

src/js/select2/data/base.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
define([
2+
'../utils'
3+
], function (Utils) {
4+
function BaseAdapter ($element, options) {
5+
BaseAdapter.__super__.constructor.call(this);
6+
}
7+
8+
Utils.Extend(BaseAdapter, Utils.Observable);
9+
10+
BaseAdapter.prototype.current = function (callback) {
11+
throw new Error("The `current` method must be defined in child classes.");
12+
}
13+
14+
BaseAdapter.prototype.query = function (params, callback) {
15+
throw new Error("The `query` method must be defined in child classes.");
16+
}
17+
18+
return BaseAdapter;
19+
});

src/js/select2/data/select.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
define([
2+
'./base',
23
'../utils',
34
'jquery'
4-
], function (Utils, $) {
5+
], function (BaseAdapter, Utils, $) {
56
function SelectAdapter ($element, options) {
67
this.$element = $element;
78

89
SelectAdapter.__super__.constructor.call(this);
910
}
1011

11-
Utils.Extend(SelectAdapter, Utils.Observable);
12+
Utils.Extend(SelectAdapter, BaseAdapter);
1213

1314
SelectAdapter.prototype.current = function (callback) {
1415
var data = [];

tests/data/base-tests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module("Data adapters - Base")
2+
3+
var BaseData = require("select2/data/base");
4+
var $ = require("jquery");
5+
var Options = require("select2/options");
6+
7+
var options = new Options({});
8+
9+
test("current is required", function (assert) {
10+
var data = new BaseData($("#qunit-fixture select"), options);
11+
12+
assert.throws(
13+
function () {
14+
data.current(function () {});
15+
},
16+
"current has no default implementation"
17+
)
18+
});
19+
20+
test("query is required", function (assert) {
21+
var data = new BaseData($("#qunit-fixture select"), options);
22+
23+
assert.throws(
24+
function () {
25+
data.query({}, function () {});
26+
},
27+
"query has no default implementation"
28+
);
29+
});

tests/data/base.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="../vendor/qunit-1.14.0.css" type="text/css" />
5+
<link rel="stylesheet" href="../../dist/css/select2.css" type="text/css" />
6+
</head>
7+
<body>
8+
<div id="qunit"></div>
9+
<div id="qunit-fixture">
10+
<select></select>
11+
</div>
12+
13+
<script src="../vendor/qunit-1.14.0.js" type="text/javascript"></script>
14+
<script src="../../vendor/almond-0.2.9.js" type="text/javascript"></script>
15+
<script src="../../vendor/jquery-2.1.0.js" type="text/javascript"></script>
16+
<script src="../../dist/js/select2.amd.js" type="text/javascript"></script>
17+
18+
<script src="base-tests.js" type="text/javascript"></script>
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)