1
0
mirror of synced 2024-11-26 23:06:02 +03:00

Broke out a base data adapter

This should allow us to create a basic interface that all adapters
must follow.
This commit is contained in:
Kevin Brown 2014-09-21 15:05:26 -04:00
parent 08ac13d510
commit 114732ec25
8 changed files with 163 additions and 10 deletions

View File

@ -136,17 +136,38 @@ define('select2/utils',[], function () {
return Utils; 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',[ define('select2/data/select',[
'./base',
'../utils', '../utils',
'jquery' 'jquery'
], function (Utils, $) { ], function (BaseAdapter, Utils, $) {
function SelectAdapter ($element, options) { function SelectAdapter ($element, options) {
this.$element = $element; this.$element = $element;
SelectAdapter.__super__.constructor.call(this); SelectAdapter.__super__.constructor.call(this);
} }
Utils.Extend(SelectAdapter, Utils.Observable); Utils.Extend(SelectAdapter, BaseAdapter);
SelectAdapter.prototype.current = function (callback) { SelectAdapter.prototype.current = function (callback) {
var data = []; var data = [];

View File

@ -136,17 +136,38 @@ define('select2/utils',[], function () {
return Utils; 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',[ define('select2/data/select',[
'./base',
'../utils', '../utils',
'jquery' 'jquery'
], function (Utils, $) { ], function (BaseAdapter, Utils, $) {
function SelectAdapter ($element, options) { function SelectAdapter ($element, options) {
this.$element = $element; this.$element = $element;
SelectAdapter.__super__.constructor.call(this); SelectAdapter.__super__.constructor.call(this);
} }
Utils.Extend(SelectAdapter, Utils.Observable); Utils.Extend(SelectAdapter, BaseAdapter);
SelectAdapter.prototype.current = function (callback) { SelectAdapter.prototype.current = function (callback) {
var data = []; var data = [];

View File

@ -9673,17 +9673,38 @@ define('select2/utils',[], function () {
return Utils; 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',[ define('select2/data/select',[
'./base',
'../utils', '../utils',
'jquery' 'jquery'
], function (Utils, $) { ], function (BaseAdapter, Utils, $) {
function SelectAdapter ($element, options) { function SelectAdapter ($element, options) {
this.$element = $element; this.$element = $element;
SelectAdapter.__super__.constructor.call(this); SelectAdapter.__super__.constructor.call(this);
} }
Utils.Extend(SelectAdapter, Utils.Observable); Utils.Extend(SelectAdapter, BaseAdapter);
SelectAdapter.prototype.current = function (callback) { SelectAdapter.prototype.current = function (callback) {
var data = []; var data = [];

25
dist/js/select2.js vendored
View File

@ -564,17 +564,38 @@ define('select2/utils',[], function () {
return Utils; 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',[ define('select2/data/select',[
'./base',
'../utils', '../utils',
'jquery' 'jquery'
], function (Utils, $) { ], function (BaseAdapter, Utils, $) {
function SelectAdapter ($element, options) { function SelectAdapter ($element, options) {
this.$element = $element; this.$element = $element;
SelectAdapter.__super__.constructor.call(this); SelectAdapter.__super__.constructor.call(this);
} }
Utils.Extend(SelectAdapter, Utils.Observable); Utils.Extend(SelectAdapter, BaseAdapter);
SelectAdapter.prototype.current = function (callback) { SelectAdapter.prototype.current = function (callback) {
var data = []; var data = [];

19
src/js/select2/data/base.js vendored Normal file
View File

@ -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;
});

View File

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

29
tests/data/base-tests.js Normal file
View File

@ -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
tests/data/base.html Normal file
View File

@ -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>