Skip to content

Commit

Permalink
fix: update lastKnownState before invoking callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jul 3, 2015
1 parent acea4f4 commit 8904cfd
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 96 deletions.
4 changes: 3 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "router5",
"version": "0.0.1-alpha.6",
"version": "0.0.1-alpha.7",
"homepage": "http://router5.github.io",
"authors": [
"Thomas Roch <thomas.c.roch@gmail.com>"
Expand All @@ -26,6 +26,8 @@
"scripts",
"npm-debug.log",
"tests",
"bower.json",
"README.md",
".travis.yml",
"karma.config.js",
"package.json",
Expand Down
46 changes: 24 additions & 22 deletions dist/browser/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,30 +534,36 @@ var Router5 = (function () {
value: function _transition(toState, fromState) {
var _this2 = this;

var cannotDeactivate = false;
if (!fromState) {
this.lastKnownState = toState;
this._invokeCallbacks('', toState, fromState);
return true;
}

if (fromState) {
var i = undefined;
var fromStateIds = nameToIDs(fromState.name);
var toStateIds = nameToIDs(toState.name);
var i = undefined;
var cannotDeactivate = false;
var fromStateIds = nameToIDs(fromState.name);
var toStateIds = nameToIDs(toState.name);
var maxI = Math.min(fromStateIds.length, toStateIds.length);

var maxI = Math.min(fromStateIds.length, toStateIds.length);
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
return !comp.canDeactivate(toState, fromState);
});
cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
return !comp.canDeactivate(toState, fromState);
});

if (!cannotDeactivate && i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState);
if (!cannotDeactivate) {
this.lastKnownState = toState;
if (i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState);
this._invokeCallbacks('', toState, fromState);
}

if (!cannotDeactivate) this._invokeCallbacks('', toState, fromState);
return !cannotDeactivate;
}
}, {
Expand Down Expand Up @@ -643,10 +649,6 @@ var Router5 = (function () {
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}
if (canTransition) {
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt;
}
}
}]);

Expand Down
10 changes: 5 additions & 5 deletions dist/browser/router5.min.js

Large diffs are not rendered by default.

50 changes: 26 additions & 24 deletions dist/commonjs/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,36 @@ var Router5 = (function () {
value: function _transition(toState, fromState) {
var _this2 = this;

if (!fromState) {
this.lastKnownState = toState;
this._invokeCallbacks('', toState, fromState);
return true;
}

var i = undefined;
var cannotDeactivate = false;
var fromStateIds = nameToIDs(fromState.name);
var toStateIds = nameToIDs(toState.name);
var maxI = Math.min(fromStateIds.length, toStateIds.length);

if (fromState) {
var i = undefined;
var fromStateIds = nameToIDs(fromState.name);
var toStateIds = nameToIDs(toState.name);

var maxI = Math.min(fromStateIds.length, toStateIds.length);
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
return !comp.canDeactivate(toState, fromState);
});

if (!cannotDeactivate && i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState);
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
return !comp.canDeactivate(toState, fromState);
});

if (!cannotDeactivate) {
this.lastKnownState = toState;
if (i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState);
this._invokeCallbacks('', toState, fromState);
}

if (!cannotDeactivate) this._invokeCallbacks('', toState, fromState);
return !cannotDeactivate;
}
}, {
Expand Down Expand Up @@ -223,10 +229,6 @@ var Router5 = (function () {
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}
if (canTransition) {
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt;
}
}
}]);

Expand Down
50 changes: 26 additions & 24 deletions dist/umd/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,36 @@
value: function _transition(toState, fromState) {
var _this2 = this;

if (!fromState) {
this.lastKnownState = toState;
this._invokeCallbacks('', toState, fromState);
return true;
}

var i = undefined;
var cannotDeactivate = false;
var fromStateIds = nameToIDs(fromState.name);
var toStateIds = nameToIDs(toState.name);
var maxI = Math.min(fromStateIds.length, toStateIds.length);

if (fromState) {
var i = undefined;
var fromStateIds = nameToIDs(fromState.name);
var toStateIds = nameToIDs(toState.name);

var maxI = Math.min(fromStateIds.length, toStateIds.length);
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
return !comp.canDeactivate(toState, fromState);
});

if (!cannotDeactivate && i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState);
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break;
}

cannotDeactivate = fromStateIds.slice(i).reverse().map(function (id) {
return _this2.activeComponents[id];
}).filter(function (comp) {
return comp && comp.canDeactivate;
}).some(function (comp) {
return !comp.canDeactivate(toState, fromState);
});

if (!cannotDeactivate) {
this.lastKnownState = toState;
if (i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState);
this._invokeCallbacks('', toState, fromState);
}

if (!cannotDeactivate) this._invokeCallbacks('', toState, fromState);
return !cannotDeactivate;
}
}, {
Expand Down Expand Up @@ -230,10 +236,6 @@
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}
if (canTransition) {
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt;
}
}
}]);

Expand Down
41 changes: 22 additions & 19 deletions modules/Router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,35 @@ export default class Router5 {
}

_transition(toState, fromState) {
if (!fromState) {
this.lastKnownState = toState
this._invokeCallbacks('', toState, fromState)
return true
}

let i
let cannotDeactivate = false
let fromStateIds = nameToIDs(fromState.name)
let toStateIds = nameToIDs(toState.name)
let maxI = Math.min(fromStateIds.length, toStateIds.length)

if (fromState) {
let i
let fromStateIds = nameToIDs(fromState.name)
let toStateIds = nameToIDs(toState.name)
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break
}

let maxI = Math.min(fromStateIds.length, toStateIds.length)
for (i = 0; i < maxI; i += 1) {
if (fromStateIds[i] !== toStateIds[i]) break
}
cannotDeactivate =
fromStateIds.slice(i).reverse()
.map(id => this.activeComponents[id])
.filter(comp => comp && comp.canDeactivate)
.some(comp => !comp.canDeactivate(toState, fromState))

cannotDeactivate =
fromStateIds.slice(i).reverse()
.map(id => this.activeComponents[id])
.filter(comp => comp && comp.canDeactivate)
.some(comp => !comp.canDeactivate(toState, fromState))

if (!cannotDeactivate && i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState)
if (!cannotDeactivate) {
this.lastKnownState = toState
if (i > 0) this._invokeCallbacks(fromStateIds[i - 1], toState, fromState)
this._invokeCallbacks('', toState, fromState)
}

if (!cannotDeactivate) this._invokeCallbacks('', toState, fromState)
return !cannotDeactivate
}

Expand Down Expand Up @@ -172,9 +179,5 @@ export default class Router5 {
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? `#${path}` : path)
}
if (canTransition) {
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "router5",
"version": "0.0.1-alpha.6",
"version": "0.0.1-alpha.7",
"description": "An HTML5 router, based on route-node and path-parser",
"main": "dist/commonjs/index.js",
"scripts": {
Expand Down

0 comments on commit 8904cfd

Please sign in to comment.