Skip to content
This repository has been archived by the owner on Dec 19, 2017. It is now read-only.

Commit

Permalink
Fix back button issue with deeply nested routers
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Oct 3, 2016
1 parent ba4dcd1 commit 7aae3d3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
21 changes: 19 additions & 2 deletions dist/ko-component-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ return /******/ (function(modules) { // webpackBootstrap

if (isRoot) {
_knockout2.default.router = this;
_knockout2.default.router.history = _knockout2.default.observableArray([]);
} else {
this.$parent = parentRouterBindingCtx.$router;
this.$parent.$child = this;
Expand Down Expand Up @@ -892,7 +893,19 @@ return /******/ (function(modules) { // webpackBootstrap

var samePage = this.pathname() === pathname;

var shouldNavigatePromise = samePage ? this.$child ? this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false) : Promise.resolve(true) : this.runBeforeNavigateCallbacks();
var shouldNavigatePromise = function () {
if (samePage) {
if (_this2.$child) {
var _push = push;
push = false;
return _this2.$child._update(childPath || '/', viaPathBinding ? state : false, _push, viaPathBinding ? query : false);
} else {
return Promise.resolve(true);
}
} else {
return _this2.runBeforeNavigateCallbacks();
}
}();

return shouldNavigatePromise.then(function (shouldNavigate) {
if (!shouldNavigate) {
Expand Down Expand Up @@ -933,7 +946,11 @@ return /******/ (function(modules) { // webpackBootstrap
}

if (!samePage || !(0, _utils.deepEquals)(fromCtx.query, toCtx.query)) {
history[push ? 'pushState' : 'replaceState'](history.state, document.title, '' === canonicalPath ? _this2.getBase() : canonicalPath);
var _path = '' === canonicalPath ? _this2.getBase() : canonicalPath;

push ? _knockout2.default.router.history.push([history.state, _path]) : _knockout2.default.router.history.splice(_knockout2.default.router.history.length - 1, 1, [history.state, _path]);

history[push ? 'pushState' : 'replaceState'](history.state, document.title, _path);
}

return new Promise(function (resolve) {
Expand Down
4 changes: 2 additions & 2 deletions dist/ko-component-router.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions example/dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/dist/bundle.js.map

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var Context = function () {

if (isRoot) {
_knockout2.default.router = this;
_knockout2.default.router.history = _knockout2.default.observableArray([]);
} else {
this.$parent = parentRouterBindingCtx.$router;
this.$parent.$child = this;
Expand Down Expand Up @@ -128,7 +129,19 @@ var Context = function () {

var samePage = this.pathname() === pathname;

var shouldNavigatePromise = samePage ? this.$child ? this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false) : Promise.resolve(true) : this.runBeforeNavigateCallbacks();
var shouldNavigatePromise = function () {
if (samePage) {
if (_this2.$child) {
var _push = push;
push = false;
return _this2.$child._update(childPath || '/', viaPathBinding ? state : false, _push, viaPathBinding ? query : false);
} else {
return Promise.resolve(true);
}
} else {
return _this2.runBeforeNavigateCallbacks();
}
}();

return shouldNavigatePromise.then(function (shouldNavigate) {
if (!shouldNavigate) {
Expand Down Expand Up @@ -169,7 +182,11 @@ var Context = function () {
}

if (!samePage || !(0, _utils.deepEquals)(fromCtx.query, toCtx.query)) {
history[push ? 'pushState' : 'replaceState'](history.state, document.title, '' === canonicalPath ? _this2.getBase() : canonicalPath);
var _path = '' === canonicalPath ? _this2.getBase() : canonicalPath;

push ? _knockout2.default.router.history.push([history.state, _path]) : _knockout2.default.router.history.splice(_knockout2.default.router.history.length - 1, 1, [history.state, _path]);

history[push ? 'pushState' : 'replaceState'](history.state, document.title, _path);
}

return new Promise(function (resolve) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ko-component-router",
"version": "3.10.12",
"version": "3.10.13",
"description": "Component-based routing for KnockoutJS",
"homepage": "https://github.com/Profiscience/ko-component-router",
"bugs": "https://github.com/Profiscience/ko-component-router/issues",
Expand Down
29 changes: 22 additions & 7 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class Context {

if (isRoot) {
ko.router = this
ko.router.history = ko.observableArray([])
} else {
this.$parent = parentRouterBindingCtx.$router
this.$parent.$child = this
Expand Down Expand Up @@ -79,12 +80,20 @@ export default class Context {
const [path, params, hash, pathname, querystring, childPath] = route.parse(url)
const samePage = this.pathname() === pathname

const shouldNavigatePromise =
samePage
? this.$child
? this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false)
: Promise.resolve(true)
: this.runBeforeNavigateCallbacks()

const shouldNavigatePromise = (() => {
if (samePage) {
if (this.$child) {
const _push = push
push = false
return this.$child._update(childPath || '/', viaPathBinding ? state : false, _push, viaPathBinding ? query : false)
} else {
return Promise.resolve(true)
}
} else {
return this.runBeforeNavigateCallbacks()
}
})()

return shouldNavigatePromise.then((shouldNavigate) => {
if (!shouldNavigate) {
Expand Down Expand Up @@ -131,10 +140,16 @@ export default class Context {
}

if (!samePage || !deepEquals(fromCtx.query, toCtx.query)) {
const path = '' === canonicalPath ? this.getBase() : canonicalPath

push
? ko.router.history.push([history.state, path])
: ko.router.history.splice(ko.router.history.length - 1, 1, [history.state, path])

history[push ? 'pushState' : 'replaceState'](
history.state,
document.title,
'' === canonicalPath ? this.getBase() : canonicalPath)
path)
}

return new Promise((resolve) => {
Expand Down

0 comments on commit 7aae3d3

Please sign in to comment.