-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add call helper * add test * add readme * add export
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 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,27 @@ | ||
import Helper from '@ember/component/helper'; | ||
|
||
/** | ||
* Calls a function passed within a template and returns its value. | ||
* In order to pass arguments to the function being called, you must | ||
* curry the function using the `fn` helper. | ||
* | ||
```example | ||
<div data-metrics={{call (fn this.myMetrics (hash item=@item))}} | ||
``` | ||
* | ||
* @function apply | ||
* @param {Array<Function>} fn - The function to be called | ||
* @param {*=} thisArg - An optional `this` context | ||
*/ | ||
export function call([fn, thisArg]) { | ||
if (fn) { | ||
if (thisArg) { | ||
return fn.apply(thisArg); | ||
} else { | ||
return fn(); | ||
} | ||
} | ||
} | ||
|
||
export default Helper.helper(call); | ||
|
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 @@ | ||
export { default, call } from 'ember-composable-helpers/helpers/call'; |
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,33 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Helper | call', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders a function return value with curried arguments', async function (assert) { | ||
this.fn = function (value) { | ||
return value.toUpperCase(); | ||
}; | ||
|
||
await render(hbs`{{call (fn this.fn "foo")}}`); | ||
|
||
assert.dom(this.element).hasText('FOO'); | ||
}); | ||
|
||
test('it calls a function with this binding', async function (assert) { | ||
this.that = { | ||
value: 'foo', | ||
}; | ||
|
||
this.fn = function () { | ||
return this.value.toUpperCase(); | ||
}; | ||
|
||
await render(hbs`{{call this.fn this.that}}`); | ||
|
||
assert.dom(this.element).hasText('FOO'); | ||
}); | ||
}); | ||
|