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

Commit

Permalink
Improve child router initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyWebb committed Oct 31, 2016
1 parent 941e2d9 commit 4782808
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 57 deletions.
46 changes: 31 additions & 15 deletions dist/ko-component-router.js
Expand Up @@ -176,7 +176,17 @@ return /******/ (function(modules) { // webpackBootstrap
if (dispatch) {
var path = this.config.hashbang && ~location.hash.indexOf('#!') ? location.hash.substr(2) + location.search : location.pathname + location.search + location.hash;

this.ctx._update(path, undefined, false);
var state = false;
var query = false;

if (!isRoot) {
state = this.ctx.$parent._$childInitState;
query = this.ctx.$parent._$childInitQuery;
delete this.ctx.$parent._$childInitState;
delete this.ctx.$parent._$childInitQuery;
}

this.ctx._update(path, state, false, query);
}
}

Expand Down Expand Up @@ -953,12 +963,11 @@ return /******/ (function(modules) { // webpackBootstrap
value: function _update() {
var origUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.canonicalPath();
var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var push = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;

var _this2 = this;

var push = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var query = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var viaPathBinding = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;

var url = this.resolveUrl(origUrl);
var route = this.getRouteForUrl(url);
Expand Down Expand Up @@ -989,7 +998,11 @@ return /******/ (function(modules) { // webpackBootstrap
if (_this2.$child) {
var _push = push;
push = false;
return _this2.$child._update(childPath || '/', viaPathBinding ? state : false, _push, viaPathBinding ? query : false);
return _this2.$child._update(childPath || '/', state, _push, query).then(function (handled) {
state = false;
query = false;
return !handled;
});
} else {
return Promise.resolve(true);
}
Expand Down Expand Up @@ -1038,19 +1051,25 @@ return /******/ (function(modules) { // webpackBootstrap
canonicalPath: canonicalPath,
hash: hash,
params: params,
query: query,
// route must come last
route: route
};

if (state === false && samePage) {
toCtx.state = fromCtx.state;
} else if (!_this2.config.persistState && state) {
toCtx.state = state;
}
if (childPath && !samePage) {
toCtx.state = {};
toCtx.query = {};
_this2._$childInitState = state;
_this2._$childInitQuery = query;
} else {
if (state === false && samePage) {
toCtx.state = fromCtx.state;
} else if (!_this2.config.persistState && state) {
toCtx.state = state;
}

if (_this2.config.persistState) {
toCtx.state = _this2.state();
if (_this2.config.persistState) {
toCtx.state = _this2.state();
}
}

if (!samePage || !(0, _utils.deepEquals)(fromCtx.query, toCtx.query)) {
Expand Down Expand Up @@ -1093,9 +1112,6 @@ return /******/ (function(modules) { // webpackBootstrap
return _this2.config.inTransition(el, fromCtx, toCtx);
});
}
if (_this2.$child) {
_this2.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false);
}
});
};

Expand Down
4 changes: 2 additions & 2 deletions dist/ko-component-router.min.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 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.

34 changes: 20 additions & 14 deletions lib/context.js
Expand Up @@ -97,12 +97,11 @@ var Context = function () {
value: function _update() {
var origUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.canonicalPath();
var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var push = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;

var _this2 = this;

var push = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var query = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var viaPathBinding = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;

var url = this.resolveUrl(origUrl);
var route = this.getRouteForUrl(url);
Expand Down Expand Up @@ -133,7 +132,11 @@ var Context = function () {
if (_this2.$child) {
var _push = push;
push = false;
return _this2.$child._update(childPath || '/', viaPathBinding ? state : false, _push, viaPathBinding ? query : false);
return _this2.$child._update(childPath || '/', state, _push, query).then(function (handled) {
state = false;
query = false;
return !handled;
});
} else {
return Promise.resolve(true);
}
Expand Down Expand Up @@ -182,19 +185,25 @@ var Context = function () {
canonicalPath: canonicalPath,
hash: hash,
params: params,
query: query,
// route must come last
route: route
};

if (state === false && samePage) {
toCtx.state = fromCtx.state;
} else if (!_this2.config.persistState && state) {
toCtx.state = state;
}
if (childPath && !samePage) {
toCtx.state = {};
toCtx.query = {};
_this2._$childInitState = state;
_this2._$childInitQuery = query;
} else {
if (state === false && samePage) {
toCtx.state = fromCtx.state;
} else if (!_this2.config.persistState && state) {
toCtx.state = state;
}

if (_this2.config.persistState) {
toCtx.state = _this2.state();
if (_this2.config.persistState) {
toCtx.state = _this2.state();
}
}

if (!samePage || !(0, _utils.deepEquals)(fromCtx.query, toCtx.query)) {
Expand Down Expand Up @@ -237,9 +246,6 @@ var Context = function () {
return _this2.config.inTransition(el, fromCtx, toCtx);
});
}
if (_this2.$child) {
_this2.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false);
}
});
};

Expand Down
12 changes: 11 additions & 1 deletion lib/router.js
Expand Up @@ -88,7 +88,17 @@ var Router = function () {
if (dispatch) {
var path = this.config.hashbang && ~location.hash.indexOf('#!') ? location.hash.substr(2) + location.search : location.pathname + location.search + location.hash;

this.ctx._update(path, undefined, false);
var state = false;
var query = false;

if (!isRoot) {
state = this.ctx.$parent._$childInitState;
query = this.ctx.$parent._$childInitQuery;
delete this.ctx.$parent._$childInitState;
delete this.ctx.$parent._$childInitQuery;
}

this.ctx._update(path, state, false, query);
}
}

Expand Down
34 changes: 21 additions & 13 deletions src/context.js
Expand Up @@ -67,7 +67,7 @@ export default class Context {
})
}

_update(origUrl = this.canonicalPath(), state = false, push = true, query = false, viaPathBinding = false) {
_update(origUrl = this.canonicalPath(), state = false, push = true, query = false) {
const url = this.resolveUrl(origUrl)
const route = this.getRouteForUrl(url)
const firstRun = this.route() === ''
Expand All @@ -86,7 +86,12 @@ export default class Context {
if (this.$child) {
const _push = push
push = false
return this.$child._update(childPath || '/', viaPathBinding ? state : false, _push, viaPathBinding ? query : false)
return this.$child._update(childPath || '/', state, _push, query)
.then((handled) => {
state = false
query = false
return !handled
})
} else {
return Promise.resolve(true)
}
Expand Down Expand Up @@ -141,19 +146,25 @@ export default class Context {
canonicalPath,
hash,
params,
query,
// route must come last
route
}

if (state === false && samePage) {
toCtx.state = fromCtx.state
} else if (!this.config.persistState && state) {
toCtx.state = state
}
if (childPath && !samePage) {
toCtx.state = {}
toCtx.query = {}
this._$childInitState = state
this._$childInitQuery = query
} else {
if (state === false && samePage) {
toCtx.state = fromCtx.state
} else if (!this.config.persistState && state) {
toCtx.state = state
}

if (this.config.persistState) {
toCtx.state = this.state()
if (this.config.persistState) {
toCtx.state = this.state()
}
}

if (!samePage || !deepEquals(fromCtx.query, toCtx.query)) {
Expand Down Expand Up @@ -201,9 +212,6 @@ export default class Context {
if (animate) {
ko.tasks.schedule(() => this.config.inTransition(el, fromCtx, toCtx))
}
if (this.$child) {
this.$child._update(childPath || '/', viaPathBinding ? state : false, false, viaPathBinding ? query : false)
}
})
}

Expand Down
12 changes: 11 additions & 1 deletion src/router.js
Expand Up @@ -58,7 +58,17 @@ class Router {
? location.hash.substr(2) + location.search
: location.pathname + location.search + location.hash

this.ctx._update(path, undefined, false)
let state = false
let query = false

if (!isRoot) {
state = this.ctx.$parent._$childInitState
query = this.ctx.$parent._$childInitQuery
delete this.ctx.$parent._$childInitState
delete this.ctx.$parent._$childInitQuery
}

this.ctx._update(path, state, false, query)
}
}

Expand Down

0 comments on commit 4782808

Please sign in to comment.