@@ -2073,19 +2073,35 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
20732073 }
20742074 }
20752075 } ) ;
2076- this . listenTo ( column , "change:direction" , this . resetCellDirection ) ;
2076+ this . listenTo ( column , "change:direction" , this . setCellDirectionMaybe ) ;
20772077 this . listenTo ( column , "change:name change:label" , this . render ) ;
20782078
20792079 if ( Backgrid . callByNeed ( column . editable ( ) , column , collection ) ) $el . addClass ( "editable" ) ;
20802080 if ( Backgrid . callByNeed ( column . sortable ( ) , column , collection ) ) $el . addClass ( "sortable" ) ;
20812081 if ( Backgrid . callByNeed ( column . renderable ( ) , column , collection ) ) $el . addClass ( "renderable" ) ;
2082+
2083+ this . listenTo ( collection , "backgrid:sort" , this . removeCellDirectionMaybe ) ;
2084+ } ,
2085+
2086+ /**
2087+ Event handler for the collection's `backgrid:sort` event. Removes all the
2088+ CSS direction classes if the column being sorted on is not the same as this
2089+ header cell's.
2090+ */
2091+ removeCellDirectionMaybe : function ( columnToSort ) {
2092+ if ( columnToSort . cid != this . column . cid ) {
2093+ this . $el . removeClass ( "ascending" ) . removeClass ( "descending" ) ;
2094+ this . column . set ( "direction" , null ) ;
2095+ }
20822096 } ,
20832097
20842098 /**
2085- Event handler for the column's `change:direction` event. Resets this cell's
2086- direction to default if sorting is being done on another column.
2099+ Event handler for the column's `change:direction` event. If this
2100+ HeaderCell's column is being sorted on, it applies the direction given as a
2101+ CSS class to the header cell. Removes all the CSS direction classes
2102+ otherwise.
20872103 */
2088- resetCellDirection : function ( columnToSort , direction ) {
2104+ setCellDirectionMaybe : function ( columnToSort , direction ) {
20892105 this . $el . removeClass ( "ascending" ) . removeClass ( "descending" ) ;
20902106 if ( columnToSort . cid == this . column . cid ) this . $el . addClass ( direction ) ;
20912107 } ,
@@ -2490,6 +2506,10 @@ var Body = Backgrid.Body = Backbone.View.extend({
24902506 */
24912507 sort : function ( column , direction ) {
24922508
2509+ if ( ! _ . contains ( [ "ascending" , "descending" , null ] , direction ) ) {
2510+ throw new RangeError ( 'direction must be one of "ascending", "descending" or `null`' ) ;
2511+ }
2512+
24932513 if ( _ . isString ( column ) ) column = this . columns . findWhere ( { name : column } ) ;
24942514
24952515 var collection = this . collection ;
0 commit comments