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

Commit 9f5a51d

Browse files
committed
Send a backgrid:sorted event from Body#sort
1 parent f3d39e6 commit 9f5a51d

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

lib/backgrid.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,8 +2491,8 @@ var Body = Backgrid.Body = Backbone.View.extend({
24912491
Backbone.PageableCollection, sorting will be done globally on all the pages
24922492
and the current page will then be returned.
24932493
2494-
Triggers a Backbone `backgrid:sort` event from the collection when done
2495-
with the column, direction, comparator and a reference to the collection.
2494+
Triggers a Backbone `backgrid:sorted` event from the collection when done
2495+
with the column, direction and a reference to the collection.
24962496
24972497
@param {Backgrid.Column} column
24982498
@param {null|"ascending"|"descending"} direction
@@ -2528,16 +2528,23 @@ var Body = Backgrid.Body = Backbone.View.extend({
25282528
{sortValue: column.sortValue()});
25292529

25302530
if (collection.fullCollection) {
2531+
// If order is null, pageable will remove the comparator on both sides,
2532+
// in this case the default insertion order comparator needs to be
2533+
// attached to get back to the order before sorting.
25312534
if (collection.fullCollection.comparator == null) {
25322535
collection.fullCollection.comparator = comparator;
25332536
}
25342537
collection.fullCollection.sort();
2538+
collection.trigger("backgrid:sorted", column, direction, collection);
25352539
}
2536-
else collection.fetch({reset: true});
2540+
else collection.fetch({reset: true, success: function () {
2541+
collection.trigger("backgrid:sorted", column, direction, collection);
2542+
}});
25372543
}
25382544
else {
25392545
collection.comparator = comparator;
25402546
collection.sort();
2547+
collection.trigger("backgrid:sorted", column, direction, collection);
25412548
}
25422549

25432550
column.set("direction", direction);

src/body.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ var Body = Backgrid.Body = Backbone.View.extend({
241241
Backbone.PageableCollection, sorting will be done globally on all the pages
242242
and the current page will then be returned.
243243
244-
Triggers a Backbone `backgrid:sort` event from the collection when done
245-
with the column, direction, comparator and a reference to the collection.
244+
Triggers a Backbone `backgrid:sorted` event from the collection when done
245+
with the column, direction and a reference to the collection.
246246
247247
@param {Backgrid.Column} column
248248
@param {null|"ascending"|"descending"} direction
@@ -278,16 +278,23 @@ var Body = Backgrid.Body = Backbone.View.extend({
278278
{sortValue: column.sortValue()});
279279

280280
if (collection.fullCollection) {
281+
// If order is null, pageable will remove the comparator on both sides,
282+
// in this case the default insertion order comparator needs to be
283+
// attached to get back to the order before sorting.
281284
if (collection.fullCollection.comparator == null) {
282285
collection.fullCollection.comparator = comparator;
283286
}
284287
collection.fullCollection.sort();
288+
collection.trigger("backgrid:sorted", column, direction, collection);
285289
}
286-
else collection.fetch({reset: true});
290+
else collection.fetch({reset: true, success: function () {
291+
collection.trigger("backgrid:sorted", column, direction, collection);
292+
}});
287293
}
288294
else {
289295
collection.comparator = comparator;
290296
collection.sort();
297+
collection.trigger("backgrid:sorted", column, direction, collection);
291298
}
292299

293300
column.set("direction", direction);

test/body.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,19 @@ describe("A Body", function () {
334334
expect(body.collection.at(0).get("id")).toBe(1);
335335
expect(body.collection.at(1).get("id")).toBe(2);
336336

337+
var onBackgridSortedCallArgs = [];
338+
col.on("backgrid:sorted", function () {
339+
onBackgridSortedCallArgs.push([].slice.apply(arguments));
340+
});
341+
337342
body.collection.trigger("backgrid:sort", body.columns.at(0), "descending");
338343

339344
expect(body.collection.at(0).get("id")).toBe(2);
340345
expect(body.collection.at(1).get("id")).toBe(1);
341346
expect(body.columns.at(0).get("direction"), "descending");
347+
expect(onBackgridSortedCallArgs.length).toBe(1);
348+
expect(onBackgridSortedCallArgs[0][0]).toBe(body.columns.at(0));
349+
expect(onBackgridSortedCallArgs[0][1]).toBe("descending");
342350

343351
$.ajax = oldAjax;
344352
});
@@ -365,31 +373,42 @@ describe("A Body", function () {
365373

366374
body.render();
367375

368-
col.trigger("backgrid:sort", body.columns.at(0), "ascending");
376+
var onBackgridSortedCallArgs = [];
377+
col.on("backgrid:sorted", function () {
378+
onBackgridSortedCallArgs.push([].slice.apply(arguments));
379+
});
369380

381+
col.trigger("backgrid:sort", body.columns.at(0), "ascending");
370382
expect(body.collection.toJSON()).toEqual([{id: 3}]);
371383
expect(body.columns.at(0).get("direction"), "ascending");
384+
expect(onBackgridSortedCallArgs.length).toBe(1);
385+
expect(onBackgridSortedCallArgs[0][0]).toBe(body.columns.at(0));
386+
expect(onBackgridSortedCallArgs[0][1]).toBe("ascending");
387+
expect(onBackgridSortedCallArgs[0][2]).toBe(col);
372388

373389
body.collection.getPage(2);
374-
375390
expect(body.collection.toJSON()).toEqual([{id: 2}]);
376391

377392
body.collection.getPage(3);
378-
379393
expect(body.collection.toJSON()).toEqual([{id: 1}]);
380394

381395
body.collection.getFirstPage();
382396

383397
col.trigger("backgrid:sort", body.columns.at(0), "descending");
384398
expect(body.columns.at(0).get("direction"), "descending");
385-
386399
expect(body.collection.toJSON()).toEqual([{id: 1}]);
400+
expect(onBackgridSortedCallArgs.length).toBe(2);
401+
expect(onBackgridSortedCallArgs[1][0]).toBe(body.columns.at(0));
402+
expect(onBackgridSortedCallArgs[1][1]).toBe("descending");
403+
expect(onBackgridSortedCallArgs[1][2]).toBe(col);
387404

388405
col.trigger("backgrid:sort", body.columns.at(0), null);
389406
expect(body.columns.at(0).get("direction"), null);
390-
391407
expect(body.collection.toJSON()).toEqual([{id: 2}]);
392-
408+
expect(onBackgridSortedCallArgs.length).toBe(3);
409+
expect(onBackgridSortedCallArgs[2][0]).toBe(body.columns.at(0));
410+
expect(onBackgridSortedCallArgs[2][1]).toBe(null);
411+
expect(onBackgridSortedCallArgs[2][2]).toBe(col);
393412
});
394413

395414
it("will put the next editable and renderable cell in edit mode when a save or one of the navigation commands is triggered via backgrid:edited from the collection", function () {

0 commit comments

Comments
 (0)