Jelajahi Sumber

Adding single select support and testing

Stretch 10 tahun lalu
induk
melakukan
e60ab22a85

+ 4 - 4
src/js/select2/selection/single.js

@@ -63,11 +63,11 @@ define([
     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 escapeMarkup = this.options.get('escapeMarkup');
 
-    return escapeMarkup(template(data));
+    return escapeMarkup(template(data, container));
   };
 
   SingleSelection.prototype.selectionContainer = function () {
@@ -82,9 +82,9 @@ define([
 
     var selection = data[0];
 
-    var formatted = this.display(selection);
-
     var $rendered = this.$selection.find('.select2-selection__rendered');
+    var formatted = this.display(selection, $rendered);
+
     $rendered.empty().append(formatted);
     $rendered.prop('title', selection.title || selection.text);
   };

+ 1 - 1
tests/selection/multiple-tests.js

@@ -34,7 +34,7 @@ test('display uses templateSelection', function (assert) {
 });
 
 test('templateSelection can addClass', function (assert) {
-  var called = false, found = false;
+  var called = false;
 
   var templateOptions = new Options({
     templateSelection: function (data, container) {

+ 29 - 0
tests/selection/single-tests.js

@@ -33,6 +33,35 @@ test('display uses templateSelection', function (assert) {
   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) {
   var selection = new SingleSelection(
     $('#qunit-fixture .single'),