Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimic action helper behavior for native elements #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions addon/helpers/route-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default Helper.extend({
return getOwner(this).lookup('router:main');
}).readOnly(),

compute([actionName, ...params]) {
compute([actionName, ...params], namedValue) {
let router = get(this, 'router');
assert('[ember-route-action-helper] Unable to lookup router', router);

Expand All @@ -50,10 +50,20 @@ export default Helper.extend({
assert(`[ember-route-action-helper] Unable to find action ${actionName}`, handler);
});

let processArgs = (args) => {
let valuePath = namedValue && namedValue.value;

if (valuePath && args.length > 0) {
args[0] = get(args[0], valuePath);
}

return args;
}

let routeAction = function(...invocationArgs) {
let { action, handler } = getRouteWithAction(router, actionName);
let args = params.concat(invocationArgs);
return run.join(handler, action, ...args);
return run.join(handler, action, ...processArgs(args));
};

routeAction[ACTION] = true;
Expand Down
9 changes: 9 additions & 0 deletions tests/acceptance/main-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ test('it invokes action in the current route hierarchy', function(assert) {
click('.foo-button');
andThen(() => assert.equal(findWithAssert('.foo-value').text().trim(), 'Set via route-with-action: Hello world Bob!'));
});

test('it calls action with the specified valuePath', function(assert) {
visit('/');
andThen(() => {
let optionBar = find('.option-bar');
fillIn('.foo-select', optionBar.val());
andThen(() => assert.equal(findWithAssert('.foo-value').text().trim(), 'bar'));
});
});
5 changes: 5 additions & 0 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<h2 class="foo-value">{{foo}}</h2>

<select class="foo-select" onchange={{route-action "updateFoo" value="target.value"}}>
<option value="baz">Baz</option>
<option value="bar" class="option-bar">Bar</option>
</select>

{{outlet}}