1
0
mirror of synced 2025-02-10 09:09:24 +03:00

Adding single select support and testing

This commit is contained in:
Stretch 2015-06-10 20:05:07 +10:00
parent 581af39288
commit e60ab22a85
3 changed files with 34 additions and 5 deletions

View File

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

View File

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

View File

@ -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'),