Last active
February 1, 2019 05:42
-
-
Save jordpo/9195150854f276f0e9ceb1200d123611 to your computer and use it in GitHub Desktop.
A route-action helper to expose current route handling
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
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