Skip to content

Instantly share code, notes, and snippets.

@jordpo
Last active February 1, 2019 05:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordpo/9195150854f276f0e9ceb1200d123611 to your computer and use it in GitHub Desktop.
Save jordpo/9195150854f276f0e9ceb1200d123611 to your computer and use it in GitHub Desktop.
A route-action helper to expose current route handling
import Helper from '@ember/component/helper';
import { inject as service } from '@ember-decorators/service';
import { bind } from '@ember/runloop';
import { getOwner } from '@ember/application';
function findHandler(owner, currentRouteInfo, action) {
const route = owner.lookup(`route:${currentRouteInfo.name}`);
const handler = route.actions ? route.actions[action] : route[action];
if (!handler && currentRouteInfo.parent) {
return findHandler(owner, currentRouteInfo.parent, action);
} else {
return {route, handler};
}
}
export default class RouteActionHelper extends Helper {
@service router;
compute([action, ...args], _hash) {
const {route, handler} = findHandler(getOwner(this), this.router.currentRoute, action);
if (handler) {
return bind(route, handler, ...args);
} else {
return function() {};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment