-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build initial implementation of route hooks * extend Router with Hooks and register all hooks * more route hooks tweaks * router namespace cleanup * removed unused values from registry entry (linter)
- Loading branch information
1 parent
e6dfdbc
commit 60cde7d
Showing
2 changed files
with
81 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
/** | ||
* Route Hook Methods | ||
*/ | ||
const Hooks = { | ||
_hooks: { | ||
onEnter: {}, | ||
onExit: {} | ||
}, | ||
|
||
_addHook(type, routeName, callback) { | ||
if (typeof this._hooks[type][routeName] === "undefined") { | ||
this._hooks[type][routeName] = []; | ||
} | ||
this._hooks[type][routeName].push(callback); | ||
}, | ||
|
||
onEnter(routeName, callback) { | ||
// global onEnter callback | ||
if (arguments.length === 1 && typeof arguments[0] === "function") { | ||
const cb = routeName; | ||
return this._addHook("onEnter", "GLOBAL", cb); | ||
} | ||
// route-specific onEnter callback | ||
return this._addHook("onEnter", routeName, callback); | ||
}, | ||
|
||
onExit(routeName, callback) { | ||
// global onExit callback | ||
if (arguments.length === 1 && typeof arguments[0] === "function") { | ||
const cb = routeName; | ||
return this._addHook("onExit", "GLOBAL", cb); | ||
} | ||
// route-specific onExit callback | ||
return this._addHook("onExit", routeName, callback); | ||
}, | ||
|
||
get(type, name) { | ||
const group = this._hooks[type] || {}; | ||
const callbacks = group[name]; | ||
return (typeof callbacks !== "undefined" && !!callbacks.length) ? callbacks : []; | ||
}, | ||
|
||
run(type, name, constant) { | ||
const callbacks = this.get(type, name); | ||
if (typeof callbacks !== "undefined" && !!callbacks.length) { | ||
return callbacks.forEach((callback) => { | ||
return callback(constant); | ||
}); | ||
} | ||
return null; | ||
} | ||
}; | ||
|
||
export default Hooks; |
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