forked from scoutforpets/ember-fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bump ember-cli-bable and import invoke action
- Loading branch information
Showing
4 changed files
with
21,933 additions
and
4,023 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
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,71 @@ | ||
import Mixin from '@ember/object/mixin'; | ||
import { assert } from '@ember/debug'; | ||
import { get } from '@ember/object'; | ||
|
||
const makeInvokeAction = ({ strict = false } = {}) => { | ||
return (object, actionName, ...args) => { | ||
assert('The first argument passed to invokeAction must be an object', | ||
typeof object === 'object'); | ||
|
||
let action; | ||
if (typeof actionName === 'string') { | ||
action = get(object, actionName); | ||
} else if (typeof actionName === 'function') { | ||
action = actionName; | ||
} else { | ||
assert('The second argument passed to invokeAction must be a string as actionName or a function', | ||
false); | ||
} | ||
|
||
if (typeof action === 'string') { | ||
object.sendAction(actionName, ...args); | ||
} else if (typeof action === 'function') { | ||
return action(...args); | ||
} else if (strict) { | ||
assert(`No invokable action ${actionName} was found`, false); | ||
} | ||
}; | ||
}; | ||
|
||
const getActions = (object) => { | ||
return object.actions ? object.actions : object._actions; | ||
}; | ||
|
||
const makeInvoke = ({ strict = false } = {}) => { | ||
return (object, actionName, ...args) => { | ||
let actions = getActions(object); | ||
let action = actions && actions[actionName]; | ||
|
||
if (typeof action === 'function') { | ||
return action.call(object, ...args); | ||
} else if (strict) { | ||
assert(`No invokable action ${actionName} was found`, false); | ||
} | ||
}; | ||
}; | ||
|
||
export const invokeAction = makeInvokeAction(); | ||
export const strictInvokeAction = makeInvokeAction({ strict: true }); | ||
|
||
export const invoke = makeInvoke(); | ||
export const strictInvoke = makeInvoke({ strict: true }); | ||
|
||
export const InvokeActionMixin = Mixin.create({ | ||
invokeAction() { | ||
return invokeAction(this, ...arguments); | ||
}, | ||
|
||
strictInvokeAction() { | ||
return strictInvokeAction(this, ...arguments); | ||
}, | ||
|
||
invoke() { | ||
return invoke(this, ...arguments); | ||
}, | ||
|
||
strictInvoke() { | ||
return strictInvoke(this, ...arguments); | ||
} | ||
}); | ||
|
||
export default invokeAction; |
Oops, something went wrong.