Skip to content

Commit

Permalink
Add route hooks API (#1253)
Browse files Browse the repository at this point in the history
* 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
jshimko authored and brent-hoover committed Aug 5, 2016
1 parent e6dfdbc commit 60cde7d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 21 deletions.
55 changes: 55 additions & 0 deletions client/modules/router/hooks.js
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;
47 changes: 26 additions & 21 deletions client/modules/router/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MetaData } from "/lib/api/router/metadata";
import { Session } from "meteor/session";
import { Meteor } from "meteor/meteor";
import { Tracker } from "meteor/tracker";
import Hooks from "./hooks";


// init flow-router
Expand All @@ -15,6 +16,8 @@ import { Tracker } from "meteor/tracker";
// client should wait on subs
Router.wait();

Router.Hooks = Hooks;

/**
* checkRouterPermissions
* check if user has route permissions
Expand Down Expand Up @@ -44,8 +47,6 @@ function checkRouterPermissions(context) {
return context;
}

// initialize title and meta data and check permissions
Router.triggers.enter([checkRouterPermissions, MetaData.init]);

/**
* getRouteName
Expand Down Expand Up @@ -192,32 +193,26 @@ Router.initPackageRoutes = () => {
route,
template,
layout,
workflow,
triggersEnter,
triggersExit
workflow
} = registryItem;
// get registry route name
const routeName = getRegistryRouteName(pkg.name, registryItem);

// layout option structure
const options = {
template: template,
workflow: workflow,
layout: layout
};
// console.log(registryItem);

// get registry route name
const name = getRegistryRouteName(pkg.name, registryItem);

// define new route
// we could allow the options to be passed in the registry if we need to be more flexible
const newRouteConfig = {
route: route,
route,
options: {
name: routeName,
template: options.template,
layout: options.layout,
triggersEnter: triggersEnter,
triggersExit: triggersExit,
action: () => {
ReactionLayout(options);
name,
template,
layout,
triggersEnter: Router.Hooks.get("onEnter", name),
triggersExit: Router.Hooks.get("onExit", name),
action() {
ReactionLayout({ template, workflow, layout });
}
}
};
Expand Down Expand Up @@ -307,4 +302,14 @@ Router.isActiveClassName = (routeName) => {
return routeDef === routeName ? "active" : "";
};

// Register Global Route Hooks
Meteor.startup(() => {
Router.Hooks.onEnter(checkRouterPermissions);
Router.Hooks.onEnter(MetaData.init);

Router.triggers.enter(Router.Hooks.get("onEnter", "GLOBAL"));
Router.triggers.exit(Router.Hooks.get("onExit", "GLOBAL"));
});


export default Router;

0 comments on commit 60cde7d

Please sign in to comment.