Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.

Commit 773fde0

Browse files
committed
Fix #426. Calling Grid#sort will now correctly reset the directions of the columns not being sorted on.
1 parent 8203e6c commit 773fde0

3 files changed

Lines changed: 21 additions & 29 deletions

File tree

lib/backgrid.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,26 +2069,23 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
20692069
}
20702070
}
20712071
});
2072-
this.listenTo(column, "change:direction", this.setCellDirectionMaybe);
2072+
this.listenTo(column, "change:direction", this.setCellDirection);
20732073
this.listenTo(column, "change:name change:label", this.render);
20742074

20752075
if (Backgrid.callByNeed(column.editable(), column, collection)) $el.addClass("editable");
20762076
if (Backgrid.callByNeed(column.sortable(), column, collection)) $el.addClass("sortable");
20772077
if (Backgrid.callByNeed(column.renderable(), column, collection)) $el.addClass("renderable");
20782078

2079-
this.listenTo(collection, "backgrid:sort", this.removeCellDirectionMaybe);
2079+
this.listenTo(collection, "sort", this.removeCellDirection);
20802080
},
20812081

20822082
/**
2083-
Event handler for the collection's `backgrid:sort` event. Removes all the
2084-
CSS direction classes if the column being sorted on is not the same as this
2085-
header cell's.
2083+
Event handler for the collection's `sort` event. Removes all the CSS
2084+
direction classes.
20862085
*/
2087-
removeCellDirectionMaybe: function (columnToSort) {
2088-
if (columnToSort.cid != this.column.cid) {
2089-
this.$el.removeClass("ascending").removeClass("descending");
2090-
this.column.set("direction", null);
2091-
}
2086+
removeCellDirection: function () {
2087+
this.$el.removeClass("ascending").removeClass("descending");
2088+
this.column.set("direction", null);
20922089
},
20932090

20942091
/**
@@ -2097,9 +2094,9 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
20972094
CSS class to the header cell. Removes all the CSS direction classes
20982095
otherwise.
20992096
*/
2100-
setCellDirectionMaybe: function (columnToSort, direction) {
2097+
setCellDirection: function (column, direction) {
21012098
this.$el.removeClass("ascending").removeClass("descending");
2102-
if (columnToSort.cid == this.column.cid) this.$el.addClass(direction);
2099+
if (column.cid == this.column.cid) this.$el.addClass(direction);
21032100
},
21042101

21052102
/**

src/header.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,23 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
4949
}
5050
}
5151
});
52-
this.listenTo(column, "change:direction", this.setCellDirectionMaybe);
52+
this.listenTo(column, "change:direction", this.setCellDirection);
5353
this.listenTo(column, "change:name change:label", this.render);
5454

5555
if (Backgrid.callByNeed(column.editable(), column, collection)) $el.addClass("editable");
5656
if (Backgrid.callByNeed(column.sortable(), column, collection)) $el.addClass("sortable");
5757
if (Backgrid.callByNeed(column.renderable(), column, collection)) $el.addClass("renderable");
5858

59-
this.listenTo(collection, "backgrid:sort", this.removeCellDirectionMaybe);
59+
this.listenTo(collection, "sort", this.removeCellDirection);
6060
},
6161

6262
/**
63-
Event handler for the collection's `backgrid:sort` event. Removes all the
64-
CSS direction classes if the column being sorted on is not the same as this
65-
header cell's.
63+
Event handler for the collection's `sort` event. Removes all the CSS
64+
direction classes.
6665
*/
67-
removeCellDirectionMaybe: function (columnToSort) {
68-
if (columnToSort.cid != this.column.cid) {
69-
this.$el.removeClass("ascending").removeClass("descending");
70-
this.column.set("direction", null);
71-
}
66+
removeCellDirection: function () {
67+
this.$el.removeClass("ascending").removeClass("descending");
68+
this.column.set("direction", null);
7269
},
7370

7471
/**
@@ -77,9 +74,9 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
7774
CSS class to the header cell. Removes all the CSS direction classes
7875
otherwise.
7976
*/
80-
setCellDirectionMaybe: function (columnToSort, direction) {
77+
setCellDirection: function (column, direction) {
8178
this.$el.removeClass("ascending").removeClass("descending");
82-
if (columnToSort.cid == this.column.cid) this.$el.addClass(direction);
79+
if (column.cid == this.column.cid) this.$el.addClass(direction);
8380
},
8481

8582
/**

test/header.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,10 @@ describe("A HeaderCell", function () {
190190
expect(cell.$el.hasClass("descending")).toBe(false);
191191
});
192192

193-
it("will remove its direction CSS class if `backgrid:sort` is triggered with a different column", function () {
193+
it("will remove its direction CSS class if `sort` is triggered", function () {
194194
cell.column.set("direction", "ascending");
195-
cell.collection.trigger("backgrid:sort", new Backgrid.Column({
196-
name: "name",
197-
cell: "string"
198-
}), "descending");
195+
cell.collection.comparator = "id";
196+
cell.collection.sort();
199197
expect(cell.$el.hasClass("ascending")).toBe(false);
200198
expect(cell.$el.hasClass("descending")).toBe(false);
201199
});

0 commit comments

Comments
 (0)