From ff6f9b36cab71379920243b8a9bdc3a5e3bf8092 Mon Sep 17 00:00:00 2001 From: Behind The Math Date: Sun, 21 Jan 2018 14:16:57 -0500 Subject: [PATCH] Bug fixes and code cleanup With a lot of help from @robinnorth --- index.js | 10 ++++++---- lib/switches-selectors.js | 19 ++++++++++++------- package.json | 2 +- tests/{index.js => setup.js} | 0 4 files changed, 19 insertions(+), 12 deletions(-) rename tests/{index.js => setup.js} (100%) diff --git a/index.js b/index.js index 9000766..948f386 100644 --- a/index.js +++ b/index.js @@ -229,13 +229,15 @@ Pjax.prototype = { // Update browser history this.lastUid = this.maxUid = newUid() + + var state = this.state window.history.pushState({ - url: this.state.href, - title: this.state.options.title, + url: state.href, + title: state.options.title, uid: this.maxUid }, - this.state.options.title, - this.state.href) + state.options.title, + state.href) } this.forEachSelectors(function(el) { diff --git a/lib/switches-selectors.js b/lib/switches-selectors.js index 289e234..e55c205 100644 --- a/lib/switches-selectors.js +++ b/lib/switches-selectors.js @@ -3,6 +3,8 @@ var forEachEls = require("./foreach-els") var defaultSwitches = require("./switches") module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, options) { + var switchesQueue = []; + selectors.forEach(function(selector) { var newEls = fromEl.querySelectorAll(selector) var oldEls = toEl.querySelectorAll(selector) @@ -25,14 +27,17 @@ module.exports = function(switches, switchesOptions, selectors, fromEl, toEl, op this.log("newEl", newEl, "oldEl", oldEl) } - this.state.numPendingSwitches++ + var callback = (switches[selector]) ? + switches[selector].bind(this, oldEl, newEl, options, switchesOptions[selector]) : + defaultSwitches.outerHTML.bind(this, oldEl, newEl, options) - if (switches[selector]) { - switches[selector].bind(this)(oldEl, newEl, options, switchesOptions[selector]) - } - else { - defaultSwitches.outerHTML.bind(this)(oldEl, newEl, options) - } + switchesQueue.push(callback) }, this) }, this) + + this.state.numPendingSwitches = switchesQueue.length + + switchesQueue.forEach(function(queuedSwitch) { + queuedSwitch() + }) } diff --git a/package.json b/package.json index 7bea2a5..5d2949c 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "lint": "jscs . && jshint . --exclude-path .gitignore", "standalone": "browserify index.js --standalone Pjax > pjax.js", "build-debug": "browserify index.js --debug --standalone Pjax > pjax.js", - "tests": "tape -r ./tests/index.js ./tests/**/*.js", + "tests": "tape -r ./tests/setup.js ./tests/**/*.js", "test": "npm run lint && npm run tests | tap-spec", "coverage-tests": "npm run tests | tap-nyc", "coverage": "nyc -x \"tests/**\" npm run coverage-tests", diff --git a/tests/index.js b/tests/setup.js similarity index 100% rename from tests/index.js rename to tests/setup.js