File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -50,11 +50,11 @@ define([
5050 this . $selection . find ( '.select2-selection__rendered' ) . empty ( ) ;
5151 } ;
5252
53- MultipleSelection . prototype . display = function ( data ) {
53+ MultipleSelection . prototype . display = function ( data , container ) {
5454 var template = this . options . get ( 'templateSelection' ) ;
5555 var escapeMarkup = this . options . get ( 'escapeMarkup' ) ;
5656
57- return escapeMarkup ( template ( data ) ) ;
57+ return escapeMarkup ( template ( data , container ) ) ;
5858 } ;
5959
6060 MultipleSelection . prototype . selectionContainer = function ( ) {
@@ -81,8 +81,8 @@ define([
8181 for ( var d = 0 ; d < data . length ; d ++ ) {
8282 var selection = data [ d ] ;
8383
84- var formatted = this . display ( selection ) ;
8584 var $selection = this . selectionContainer ( ) ;
85+ var formatted = this . display ( selection , $selection ) ;
8686
8787 $selection . append ( formatted ) ;
8888 $selection . prop ( 'title' , selection . title || selection . text ) ;
Original file line number Diff line number Diff line change @@ -63,11 +63,11 @@ define([
6363 this . $selection . find ( '.select2-selection__rendered' ) . empty ( ) ;
6464 } ;
6565
66- SingleSelection . prototype . display = function ( data ) {
66+ SingleSelection . prototype . display = function ( data , container ) {
6767 var template = this . options . get ( 'templateSelection' ) ;
6868 var escapeMarkup = this . options . get ( 'escapeMarkup' ) ;
6969
70- return escapeMarkup ( template ( data ) ) ;
70+ return escapeMarkup ( template ( data , container ) ) ;
7171 } ;
7272
7373 SingleSelection . prototype . selectionContainer = function ( ) {
@@ -82,9 +82,9 @@ define([
8282
8383 var selection = data [ 0 ] ;
8484
85- var formatted = this . display ( selection ) ;
86-
8785 var $rendered = this . $selection . find ( '.select2-selection__rendered' ) ;
86+ var formatted = this . display ( selection , $rendered ) ;
87+
8888 $rendered . empty ( ) . append ( formatted ) ;
8989 $rendered . prop ( 'title' , selection . title || selection . text ) ;
9090 } ;
Original file line number Diff line number Diff line change @@ -33,6 +33,35 @@ test('display uses templateSelection', function (assert) {
3333 assert . equal ( out , 'test' ) ;
3434} ) ;
3535
36+ test ( 'templateSelection can addClass' , function ( assert ) {
37+ var called = false ;
38+
39+ var templateOptions = new Options ( {
40+ templateSelection : function ( data , container ) {
41+ called = true ;
42+ container . addClass ( 'testclass' ) ;
43+ return data . text ;
44+ }
45+ } ) ;
46+
47+ var selection = new MultipleSelection (
48+ $ ( '#qunit-fixture .multiple' ) ,
49+ templateOptions
50+ ) ;
51+
52+ var $container = selection . selectionContainer ( ) ;
53+
54+ var out = selection . display ( {
55+ text : 'test'
56+ } , $container ) ;
57+
58+ assert . ok ( called ) ;
59+
60+ assert . equal ( out , 'test' ) ;
61+
62+ assert . ok ( $container . hasClass ( 'testclass' ) ) ;
63+ } ) ;
64+
3665test ( 'empty update clears the selection' , function ( assert ) {
3766 var selection = new MultipleSelection (
3867 $ ( '#qunit-fixture .multiple' ) ,
Original file line number Diff line number Diff line change @@ -33,6 +33,35 @@ test('display uses templateSelection', function (assert) {
3333 assert . equal ( out , 'test' ) ;
3434} ) ;
3535
36+ test ( 'templateSelection can addClass' , function ( assert ) {
37+ var called = false ;
38+
39+ var templateOptions = new Options ( {
40+ templateSelection : function ( data , container ) {
41+ called = true ;
42+ container . addClass ( 'testclass' ) ;
43+ return data . text ;
44+ }
45+ } ) ;
46+
47+ var selection = new SingleSelection (
48+ $ ( '#qunit-fixture .single' ) ,
49+ templateOptions
50+ ) ;
51+
52+ var $container = selection . selectionContainer ( ) ;
53+
54+ var out = selection . display ( {
55+ text : 'test'
56+ } , $container ) ;
57+
58+ assert . ok ( called ) ;
59+
60+ assert . equal ( out , 'test' ) ;
61+
62+ assert . ok ( $container . hasClass ( 'testclass' ) ) ;
63+ } ) ;
64+
3665test ( 'empty update clears the selection' , function ( assert ) {
3766 var selection = new SingleSelection (
3867 $ ( '#qunit-fixture .single' ) ,
You can’t perform that action at this time.
0 commit comments