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

Make actions use target=this implicitly #14479

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 1 addition & 6 deletions packages/ember-glimmer/lib/helpers/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class ClosureActionReference extends CachedReference {
let { named, positional } = this.args;
let positionalValues = positional.value();

let target = positionalValues[0];
let target = named.get('target').value();
let rawActionRef = positional.at(1);
let rawAction = positionalValues[1];

Expand All @@ -305,11 +305,6 @@ export class ClosureActionReference extends CachedReference {

action = null;

if (named.has('target')) {
// on-change={{action 'setName' target=alternativeComponent}}
target = named.get('target').value();
}

if (target['actions']) {
action = target.actions[actionName];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
with

```handlebars
<button {{action this 'foo'}}>
<button onblur={{action this 'foo'}}>
<button onblur={{action this (action this 'foo') 'bar'}}>
<button {{action target=this 'foo'}}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right, the hash has to be after positional args...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL...

<button onblur={{action target=this 'foo'}}>
<button onblur={{action target=this (action target=this 'foo') 'bar'}}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this also backwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; it has been corrected.

```

If an action already has a target it is left unmodified.

@private
@class TransformActionSyntax
*/
Expand All @@ -41,16 +43,19 @@ TransformActionSyntax.prototype.transform = function TransformActionSyntax_trans
ElementModifierStatement(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
ensureTarget(node, b);
}
},
MustacheStatement(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
ensureTarget(node, b);
}
},
SubExpression(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
ensureTarget(node, b);
}
}
});
Expand All @@ -62,6 +67,24 @@ function isAction(node) {
return node.path.original === 'action';
}

function ensureTarget(node, builders) {
if (!hashPairForKey(node.hash, 'target')) {
let thisTarget = builders.pair('target', builders.path('this'));
node.hash.pairs.push(thisTarget);
}
}

function hashPairForKey(hash, key) {
for (let i = 0; i < hash.pairs.length; i++) {
let pair = hash.pairs[i];
if (pair.key === key) {
return pair;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is unfortunately repeated. @rwjblue should the util function be DRY somewhere? And if so where?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will extract it and then make another PR to update other refs.


return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is unfortunately repeated. @rwjblue should the util function be DRY somewhere? And if so where?


function insertThisAsFirstParam(node, builders) {
node.params.unshift(builders.path('this'));
}