Created
July 19, 2016 19:22
-
-
Save iheanyi/876900476abe6c70d8e65a9df2a053f9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Ember from 'ember'; | |
const ClosureActionModule = Ember.__loader.require('ember-routing-htmlbars/keywords/closure-action'); | |
export const { ACTION } = ClosureActionModule; | |
const { | |
assert, | |
getOwner, | |
typeOf, | |
Helper, | |
run | |
} = Ember; | |
export default Helper.extend({ | |
compute([actionName, targetName, ...params]) { | |
let target; | |
assert(`You cannot create a scoped-action without specifying a route or context as the second parameter.`, targetName); | |
if (typeof targetName === 'string') { | |
let route = getOwner(this).lookup(`route:${targetName}`); | |
assert(`You cannot create a scoped-action for the route '${targetName}', as it does not exist`, route); | |
let currentRoute = route.router.currentRouteName; | |
let isActive = currentRoute.indexOf(targetName) === 0 || targetName === 'application'; | |
assert(`You cannot create a scoped-action for the route '${targetName}', as it is not a parent of the active route '${currentRoute}'.`, isActive); | |
target = route; | |
} else { | |
target = targetName.target; | |
} | |
let actions = target.actions || target._actions; | |
let action = actions[actionName]; | |
assert(`You cannot create a scoped-action for the route '${targetName}', as it does not define the action '${actionName}'`, typeOf(action) === 'function'); | |
let scopedAction = (...invocationArgs) => { | |
let args = params.concat(invocationArgs); | |
return run.join(target, action, ...args); | |
}; | |
scopedAction[ACTION] = true; | |
return scopedAction; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment