If any switches are async, the subsequent code will execute before the switches are finished. This commit moves all that code to a new function, and debounces the calls to onSwitch() so it only executes once, after all the switches finish. Fizes #72.
39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
var forEachEls = require("./foreach-els")
|
||
|
||
var defaultSwitches = require("./switches")
|
||
|
||
module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, options) {
|
||
selectors.forEach(function(selector) {
|
||
var newEls = fromEl.querySelectorAll(selector)
|
||
var oldEls = toEl.querySelectorAll(selector)
|
||
if (this.log) {
|
||
this.log("Pjax switch", selector, newEls, oldEls)
|
||
}
|
||
if (newEls.length !== oldEls.length) {
|
||
// forEachEls(newEls, function(el) {
|
||
// this.log("newEl", el, el.outerHTML)
|
||
// }, this)
|
||
// forEachEls(oldEls, function(el) {
|
||
// this.log("oldEl", el, el.outerHTML)
|
||
// }, this)
|
||
throw "DOM doesn’t look the same on new loaded page: ’" + selector + "’ - new " + newEls.length + ", old " + oldEls.length
|
||
}
|
||
|
||
forEachEls(newEls, function(newEl, i) {
|
||
var oldEl = oldEls[i]
|
||
if (this.log) {
|
||
this.log("newEl", newEl, "oldEl", oldEl)
|
||
}
|
||
|
||
this.state.numPendingSwitches++
|
||
|
||
if (switches[selector]) {
|
||
switches[selector].bind(this)(oldEl, newEl, options, switchesOptions[selector])
|
||
}
|
||
else {
|
||
defaultSwitches.outerHTML.bind(this)(oldEl, newEl, options)
|
||
}
|
||
}, this)
|
||
}, this)
|
||
}
|