Skip to content
This repository was archived by the owner on May 17, 2018. It is now read-only.

Commit 2f46527

Browse files
committed
Fix #118 remove event handlers needs to be called before add handlers under client mode when remove from the current page
1 parent 1d76bfc commit 2f46527

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

lib/backbone-pageable.js

+34-19
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@
9191
return params;
9292
}
9393

94+
// hack to make sure the whatever event handlers for this event is run
95+
// before func is, and the event handlers that func will trigger.
96+
function runOnceAtLastHandler (col, event, func) {
97+
var eventHandlers = col._events[event];
98+
if (eventHandlers && eventHandlers.length) {
99+
var lastHandler = eventHandlers[eventHandlers.length - 1];
100+
var oldCallback = lastHandler.callback;
101+
lastHandler.callback = function () {
102+
try {
103+
oldCallback.apply(this, arguments);
104+
func();
105+
}
106+
catch (e) {
107+
throw e;
108+
}
109+
finally {
110+
lastHandler.callback = oldCallback;
111+
}
112+
};
113+
}
114+
else func();
115+
}
116+
94117
var PARAM_TRIM_RE = /[\s'"]/g;
95118
var URL_TRIM_RE = /[<>\s'"]/g;
96119

@@ -413,22 +436,10 @@
413436
pageCol.at(pageSize) :
414437
null;
415438
if (modelToRemove) {
416-
var addHandlers = collection._events.add || [],
417-
popOptions = {onAdd: true};
418-
if (addHandlers.length) {
419-
var lastAddHandler = addHandlers[addHandlers.length - 1];
420-
var oldCallback = lastAddHandler.callback;
421-
lastAddHandler.callback = function () {
422-
try {
423-
oldCallback.apply(this, arguments);
424-
pageCol.remove(modelToRemove, popOptions);
425-
}
426-
finally {
427-
lastAddHandler.callback = oldCallback;
428-
}
429-
};
430-
}
431-
else pageCol.remove(modelToRemove, popOptions);
439+
var popOptions = {onAdd: true};
440+
runOnceAtLastHandler(collection, event, function () {
441+
pageCol.remove(modelToRemove, popOptions);
442+
});
432443
}
433444
}
434445
}
@@ -450,7 +461,11 @@
450461

451462
var nextModel, removedIndex = options.index;
452463
if (collection == pageCol) {
453-
if (nextModel = fullCol.at(pageEnd)) pageCol.push(nextModel);
464+
if (nextModel = fullCol.at(pageEnd)) {
465+
runOnceAtLastHandler(pageCol, event, function () {
466+
pageCol.push(nextModel);
467+
});
468+
}
454469
fullCol.remove(model);
455470
}
456471
else if (removedIndex >= pageStart && removedIndex < pageEnd) {
@@ -468,13 +483,13 @@
468483
collection = model;
469484

470485
// Reset that's not a result of getPage
471-
if (collection === pageCol && options.from == null &&
486+
if (collection == pageCol && options.from == null &&
472487
options.to == null) {
473488
var head = fullCol.models.slice(0, pageStart);
474489
var tail = fullCol.models.slice(pageStart + pageCol.models.length);
475490
fullCol.reset(head.concat(pageCol.models).concat(tail), options);
476491
}
477-
else if (collection === fullCol) {
492+
else if (collection == fullCol) {
478493
if (!(state.totalRecords = fullCol.models.length)) {
479494
state.totalRecords = null;
480495
state.totalPages = null;

test/client-pageable.js

+38-11
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,52 @@ $(document).ready(function () {
325325
strictEqual(col.length, 0)
326326
});
327327

328-
test("add handlers are run before remove handlers", 2, function () {
329-
var addRan = false;
330-
var onAdd = function () {
331-
addRan = true;
332-
};
333-
var onRemove = function () {
334-
strictEqual(addRan, true);
335-
addRan = false;
336-
};
328+
test("add handlers on pageCol are run before remove handlers", 2, function () {
337329
var col = new Backbone.PageableCollection(models, {
338330
state: {
339331
pageSize: 1
340332
},
341333
mode: "client"
342334
});
343-
col.on("add", onAdd);
344-
col.on("remove", onRemove);
335+
336+
var queue = [];
337+
col.on("add", function () {
338+
queue.push("add");
339+
});
340+
col.on("remove", function () {
341+
queue.push("remove");
342+
});
343+
345344
col.unshift(new Backbone.Model({name: "d"}));
345+
deepEqual(queue, ["add", "remove"]);
346+
347+
queue = []
346348
col.fullCollection.unshift(new Backbone.Model({name: "e"}));
349+
deepEqual(queue, ["add", "remove"]);
350+
});
351+
352+
test("remove handlers on pageCol are run before add handlers", 2, function () {
353+
var col = new Backbone.PageableCollection(models, {
354+
state: {
355+
pageSize: 1
356+
},
357+
mode: "client"
358+
});
359+
360+
var queue = [];
361+
col.on("add", function () {
362+
queue.push("add");
363+
});
364+
col.on("remove", function () {
365+
queue.push("remove");
366+
});
367+
368+
col.remove(col.at(0));
369+
deepEqual(queue, ["remove", "add"]);
370+
371+
queue = [];
372+
col.fullCollection.remove(col.at(0));
373+
deepEqual(queue, ["remove", "add"]);
347374
});
348375

349376
test("change", 6, function () {

0 commit comments

Comments
 (0)