-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(resolve): Allow resolve's state context to be injected as
$state$
refactor(hooks): Move a) core resolvables and b) update globals to transition hooks. Closes angular-ui/ui-router#3273
- Loading branch information
1 parent
f7f353e
commit a06948b
Showing
7 changed files
with
90 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** @module hooks */ /** */ | ||
import { Transition } from "../transition/transition"; | ||
import { UIRouter } from "../router"; | ||
import { TransitionService } from "../transition/transitionService"; | ||
|
||
function addCoreResolvables(trans: Transition) { | ||
trans.addResolvable({ token: UIRouter, deps: [], resolveFn: () => trans.router, data: trans.router }, ""); | ||
trans.addResolvable({ token: Transition, deps: [], resolveFn: () => trans, data: trans }, ""); | ||
trans.addResolvable({ token: '$transition$', deps: [], resolveFn: () => trans, data: trans }, ""); | ||
trans.addResolvable({ token: '$stateParams', deps: [], resolveFn: () => trans.params(), data: trans.params() }, ""); | ||
|
||
trans.entering().forEach(state => { | ||
trans.addResolvable({ token: '$state$', deps: [], resolveFn: () => state, data: state }, state); | ||
}); | ||
} | ||
|
||
export const registerAddCoreResolvables= (transitionService: TransitionService) => | ||
transitionService.onCreate({}, addCoreResolvables); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** @module hooks */ /** for typedoc */ | ||
import { Transition } from "../transition/transition"; | ||
import { copy } from "../common/common"; | ||
import { Globals } from "../globals"; | ||
import { TransitionService } from "../transition/transitionService"; | ||
|
||
/** | ||
* A [[TransitionHookFn]] which updates global UI-Router state | ||
* | ||
* Registered using `transitionService.onBefore({}, updateGlobalState);` | ||
* | ||
* Before a [[Transition]] starts, updates the global value of "the current transition" ([[Globals.transition]]). | ||
* After a successful [[Transition]], updates the global values of "the current state" | ||
* ([[Globals.current]] and [[Globals.$current]]) and "the current param values" ([[Globals.params]]). | ||
* | ||
* See also the deprecated properties: | ||
* [[StateService.transition]], [[StateService.current]], [[StateService.params]] | ||
*/ | ||
const updateGlobalState = (trans: Transition) => { | ||
let globals = trans.router.globals as Globals; | ||
globals.transition = trans; | ||
globals.transitionHistory.enqueue(trans); | ||
|
||
const updateGlobalState = () => { | ||
globals.successfulTransitions.enqueue(trans); | ||
globals.$current = trans.$to(); | ||
globals.current = globals.$current.self; | ||
copy(trans.params(), globals.params); | ||
}; | ||
|
||
trans.onSuccess({}, updateGlobalState, {priority: 10000}); | ||
|
||
const clearCurrentTransition = () => { | ||
// Do not clear globals.transition if a different transition has started in the meantime | ||
if (globals.transition === trans) globals.transition = null; | ||
}; | ||
|
||
trans.promise.then(clearCurrentTransition, clearCurrentTransition); | ||
}; | ||
|
||
export const registerUpdateGlobalState = (transitionService: TransitionService) => | ||
transitionService.onBefore({}, updateGlobalState); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters