From a6a9df5b4bc86daa63d11268d3700042fcd8688f Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Mon, 15 Mar 2021 19:41:05 -0700 Subject: [PATCH] Simple bind helper This binds a function to a target before passing it along to another component. It's normal to expect to get to use `this` within functions on components and controllers, but (sans actions) that doesn't happen automatically. --- ui/app/helpers/bind.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ui/app/helpers/bind.js diff --git a/ui/app/helpers/bind.js b/ui/app/helpers/bind.js new file mode 100644 index 000000000..36e3bf4e2 --- /dev/null +++ b/ui/app/helpers/bind.js @@ -0,0 +1,17 @@ +import { helper } from '@ember/component/helper'; +import { assert } from '@ember/debug'; + +/** + * bind + * + * Usage: {{bind this.function}} + * + * Returns a version of a function bound to the template target (e.g., component or controller) + */ +export function bind([func, target]) { + assert('A function is required as the first argument', typeof func === 'function'); + assert('A context is required as the second argument', target); + return func.bind(target); +} + +export default helper(bind);