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 on nested routers
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Sep 29, 2016
1 parent 995816f commit fdfa0a4
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 82 deletions.
41 changes: 13 additions & 28 deletions dist/ko-component-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,43 +159,28 @@ return /******/ (function(modules) { // webpackBootstrap

this.ctx = new _context2.default(bindingCtx, this.config);

this.onpopstate = this.onpopstate.bind(this);
var isRoot = (0, _utils.isUndefined)(this.ctx.$parent);

this.onclick = this.onclick.bind(this);
window.addEventListener('popstate', this.onpopstate, false);
this.onpopstate = this.onpopstate.bind(this);
document.addEventListener(clickEvent, this.onclick, false);
if (isRoot) {
window.addEventListener('popstate', this.onpopstate, false);
}

var dispatch = true;
if (this.ctx.$parent) {
if (!isRoot) {
dispatch = this.ctx.$parent.path() !== this.ctx.$parent.canonicalPath();
}

if (dispatch) {
var path = this.config.hashbang && ~location.hash.indexOf('#!') ? location.hash.substr(2) + location.search : location.pathname + location.search + location.hash;

this.dispatch({ path: path });
this.ctx._update(path, undefined, false);
}
}

_createClass(Router, [{
key: 'dispatch',
value: function dispatch(_ref2) {
var path = _ref2.path;
var state = _ref2.state;
var _ref2$pushState = _ref2.pushState;
var pushState = _ref2$pushState === undefined ? false : _ref2$pushState;

var ctx = this.ctx;
while (ctx.$child) {
ctx = ctx.$child;
}

if (path.toLowerCase().indexOf(ctx.config.base.toLowerCase()) === 0) {
path = path.substr(ctx.config.base.length) || '/';
}

return ctx._update(path, state, pushState, false);
}
}, {
key: 'onpopstate',
value: function onpopstate(e) {
if (e.defaultPrevented) {
Expand All @@ -205,7 +190,7 @@ return /******/ (function(modules) { // webpackBootstrap
var path = location.pathname + location.search + location.hash;
var state = (e.state || {})[(0, _utils.normalizePath)(this.ctx.config.depth + this.ctx.pathname())];

if (this.dispatch({ path: path, state: state })) {
if (this.ctx._update(path, state, false)) {
e.preventDefault();
}
}
Expand Down Expand Up @@ -236,7 +221,7 @@ return /******/ (function(modules) { // webpackBootstrap

var path = el.pathname + el.search + (el.hash || '');

if (this.dispatch({ path: path, pushState: true })) {
if (this.ctx._update(path)) {
e.preventDefault();
}
}
Expand Down Expand Up @@ -906,7 +891,7 @@ 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 = samePage ? this.$child ? this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false) : Promise.resolve(true) : this.runBeforeNavigateCallbacks();

return shouldNavigatePromise.then(function (shouldNavigate) {
if (!shouldNavigate) {
Expand Down Expand Up @@ -982,7 +967,7 @@ return /******/ (function(modules) { // webpackBootstrap
});
}
if (_this2.$child) {
_this2.$child.update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false);
_this2.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false);
}
});
};
Expand Down Expand Up @@ -2392,7 +2377,7 @@ return /******/ (function(modules) { // webpackBootstrap
var router = _getRoute4[0];
var route = _getRoute4[1];

var handled = router.update(route, _knockout2.default.toJS(state), true, _knockout2.default.toJS(query), true);
var handled = router._update(route, _knockout2.default.toJS(state), true, _knockout2.default.toJS(query), true);

if (handled) {
e.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions dist/ko-component-router.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 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.

2 changes: 1 addition & 1 deletion lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function applyBinding(el, bindings, ctx) {
var router = _getRoute4[0];
var route = _getRoute4[1];

var handled = router.update(route, _knockout2.default.toJS(state), true, _knockout2.default.toJS(query), true);
var handled = router._update(route, _knockout2.default.toJS(state), true, _knockout2.default.toJS(query), true);

if (handled) {
e.preventDefault();
Expand Down
4 changes: 2 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ 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 = samePage ? this.$child ? this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false) : Promise.resolve(true) : this.runBeforeNavigateCallbacks();

return shouldNavigatePromise.then(function (shouldNavigate) {
if (!shouldNavigate) {
Expand Down Expand Up @@ -203,7 +203,7 @@ var Context = function () {
});
}
if (_this2.$child) {
_this2.$child.update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false);
_this2.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false);
}
});
};
Expand Down
35 changes: 10 additions & 25 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,28 @@ var Router = function () {

this.ctx = new _context2.default(bindingCtx, this.config);

this.onpopstate = this.onpopstate.bind(this);
var isRoot = (0, _utils.isUndefined)(this.ctx.$parent);

this.onclick = this.onclick.bind(this);
window.addEventListener('popstate', this.onpopstate, false);
this.onpopstate = this.onpopstate.bind(this);
document.addEventListener(clickEvent, this.onclick, false);
if (isRoot) {
window.addEventListener('popstate', this.onpopstate, false);
}

var dispatch = true;
if (this.ctx.$parent) {
if (!isRoot) {
dispatch = this.ctx.$parent.path() !== this.ctx.$parent.canonicalPath();
}

if (dispatch) {
var path = this.config.hashbang && ~location.hash.indexOf('#!') ? location.hash.substr(2) + location.search : location.pathname + location.search + location.hash;

this.dispatch({ path: path });
this.ctx._update(path, undefined, false);
}
}

_createClass(Router, [{
key: 'dispatch',
value: function dispatch(_ref2) {
var path = _ref2.path;
var state = _ref2.state;
var _ref2$pushState = _ref2.pushState;
var pushState = _ref2$pushState === undefined ? false : _ref2$pushState;

var ctx = this.ctx;
while (ctx.$child) {
ctx = ctx.$child;
}

if (path.toLowerCase().indexOf(ctx.config.base.toLowerCase()) === 0) {
path = path.substr(ctx.config.base.length) || '/';
}

return ctx._update(path, state, pushState, false);
}
}, {
key: 'onpopstate',
value: function onpopstate(e) {
if (e.defaultPrevented) {
Expand All @@ -117,7 +102,7 @@ var Router = function () {
var path = location.pathname + location.search + location.hash;
var state = (e.state || {})[(0, _utils.normalizePath)(this.ctx.config.depth + this.ctx.pathname())];

if (this.dispatch({ path: path, state: state })) {
if (this.ctx._update(path, state, false)) {
e.preventDefault();
}
}
Expand Down Expand Up @@ -148,7 +133,7 @@ var Router = function () {

var path = el.pathname + el.search + (el.hash || '');

if (this.dispatch({ path: path, pushState: true })) {
if (this.ctx._update(path)) {
e.preventDefault();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function applyBinding(el, bindings, ctx) {
}

const [router, route] = getRoute(ctx, path)
const handled = router.update(route, ko.toJS(state), true, ko.toJS(query), true)
const handled = router._update(route, ko.toJS(state), true, ko.toJS(query), true)

if (handled) {
e.preventDefault()
Expand Down
4 changes: 2 additions & 2 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class Context {
const shouldNavigatePromise =
samePage
? this.$child
? this.$child.update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false)
? this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false)
: Promise.resolve(true)
: this.runBeforeNavigateCallbacks()

Expand Down Expand Up @@ -167,7 +167,7 @@ export default class Context {
ko.tasks.schedule(() => this.config.inTransition(el, fromCtx, toCtx))
}
if (this.$child) {
this.$child.update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false)
this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false)
}
})
}
Expand Down
29 changes: 10 additions & 19 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ class Router {

this.ctx = new Context(bindingCtx, this.config)

this.onpopstate = this.onpopstate.bind(this)
const isRoot = isUndefined(this.ctx.$parent)

this.onclick = this.onclick.bind(this)
window.addEventListener('popstate', this.onpopstate, false)
this.onpopstate = this.onpopstate.bind(this)
document.addEventListener(clickEvent, this.onclick, false)
if (isRoot) {
window.addEventListener('popstate', this.onpopstate, false)
}

let dispatch = true
if (this.ctx.$parent) {
if (!isRoot) {
dispatch = this.ctx.$parent.path() !== this.ctx.$parent.canonicalPath()
}

Expand All @@ -54,21 +58,8 @@ class Router {
? location.hash.substr(2) + location.search
: location.pathname + location.search + location.hash

this.dispatch({ path })
}
}

dispatch({ path, state, pushState = false }) {
let ctx = this.ctx
while (ctx.$child) {
ctx = ctx.$child
this.ctx._update(path, undefined, false)
}

if (path.toLowerCase().indexOf(ctx.config.base.toLowerCase()) === 0) {
path = path.substr(ctx.config.base.length) || '/'
}

return ctx._update(path, state, pushState, false)
}

onpopstate(e) {
Expand All @@ -79,7 +70,7 @@ class Router {
const path = location.pathname + location.search + location.hash
const state = (e.state || {})[normalizePath(this.ctx.config.depth + this.ctx.pathname())]

if (this.dispatch({ path, state })) {
if (this.ctx._update(path, state, false)) {
e.preventDefault()
}
}
Expand Down Expand Up @@ -116,7 +107,7 @@ class Router {

const path = el.pathname + el.search + (el.hash || '')

if (this.dispatch({ path, pushState: true })) {
if (this.ctx._update(path)) {
e.preventDefault()
}
}
Expand Down

0 comments on commit fdfa0a4

Please sign in to comment.