Adding single select support and testing
This commit is contained in:
parent
581af39288
commit
e60ab22a85
8
src/js/select2/selection/single.js
vendored
8
src/js/select2/selection/single.js
vendored
@ -63,11 +63,11 @@ define([
|
|||||||
this.$selection.find('.select2-selection__rendered').empty();
|
this.$selection.find('.select2-selection__rendered').empty();
|
||||||
};
|
};
|
||||||
|
|
||||||
SingleSelection.prototype.display = function (data) {
|
SingleSelection.prototype.display = function (data, container) {
|
||||||
var template = this.options.get('templateSelection');
|
var template = this.options.get('templateSelection');
|
||||||
var escapeMarkup = this.options.get('escapeMarkup');
|
var escapeMarkup = this.options.get('escapeMarkup');
|
||||||
|
|
||||||
return escapeMarkup(template(data));
|
return escapeMarkup(template(data, container));
|
||||||
};
|
};
|
||||||
|
|
||||||
SingleSelection.prototype.selectionContainer = function () {
|
SingleSelection.prototype.selectionContainer = function () {
|
||||||
@ -82,9 +82,9 @@ define([
|
|||||||
|
|
||||||
var selection = data[0];
|
var selection = data[0];
|
||||||
|
|
||||||
var formatted = this.display(selection);
|
|
||||||
|
|
||||||
var $rendered = this.$selection.find('.select2-selection__rendered');
|
var $rendered = this.$selection.find('.select2-selection__rendered');
|
||||||
|
var formatted = this.display(selection, $rendered);
|
||||||
|
|
||||||
$rendered.empty().append(formatted);
|
$rendered.empty().append(formatted);
|
||||||
$rendered.prop('title', selection.title || selection.text);
|
$rendered.prop('title', selection.title || selection.text);
|
||||||
};
|
};
|
||||||
|
@ -34,7 +34,7 @@ test('display uses templateSelection', function (assert) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('templateSelection can addClass', function (assert) {
|
test('templateSelection can addClass', function (assert) {
|
||||||
var called = false, found = false;
|
var called = false;
|
||||||
|
|
||||||
var templateOptions = new Options({
|
var templateOptions = new Options({
|
||||||
templateSelection: function (data, container) {
|
templateSelection: function (data, container) {
|
||||||
|
@ -33,6 +33,35 @@ test('display uses templateSelection', function (assert) {
|
|||||||
assert.equal(out, 'test');
|
assert.equal(out, 'test');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('templateSelection can addClass', function (assert) {
|
||||||
|
var called = false;
|
||||||
|
|
||||||
|
var templateOptions = new Options({
|
||||||
|
templateSelection: function (data, container) {
|
||||||
|
called = true;
|
||||||
|
container.addClass('testclass');
|
||||||
|
return data.text;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var selection = new SingleSelection(
|
||||||
|
$('#qunit-fixture .single'),
|
||||||
|
templateOptions
|
||||||
|
);
|
||||||
|
|
||||||
|
var $container = selection.selectionContainer();
|
||||||
|
|
||||||
|
var out = selection.display({
|
||||||
|
text: 'test'
|
||||||
|
}, $container);
|
||||||
|
|
||||||
|
assert.ok(called);
|
||||||
|
|
||||||
|
assert.equal(out, 'test');
|
||||||
|
|
||||||
|
assert.ok($container.hasClass('testclass'));
|
||||||
|
});
|
||||||
|
|
||||||
test('empty update clears the selection', function (assert) {
|
test('empty update clears the selection', function (assert) {
|
||||||
var selection = new SingleSelection(
|
var selection = new SingleSelection(
|
||||||
$('#qunit-fixture .single'),
|
$('#qunit-fixture .single'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user