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

Refactor: {{action}} helper usage, the sequel #896

Draft
wants to merge 9 commits into
base: refactor/action_helper_usage
Choose a base branch
from

Conversation

DrumsnChocolate
Copy link
Contributor

@DrumsnChocolate DrumsnChocolate commented Nov 3, 2024

bases on #635
ref #899
may require a third PR after this

New prevent-default helper

This PR also introduces a new template helper: prevent-default. It is used to wrap the action we assign to be executed when an event takes place. Specifically, it adds a call to event.preventDefault(). Why would we do this in a helper? Well:

  1. because we don't want to pollute the js files with e.preventDefault(); calls.
  2. the decision of whether or not to call preventDefault should take place in the template, because that's where it is easiest to determine.

To elaborate on point two, when we have the following action:

@action
someAction(event) {
    someLogic();
}

Then a template with

<form {{on 'submit' someAction}}></form>

requires a call to e.preventDefault():

@action
someAction(event) {
    event.preventDefault();
    someLogic();
}

otherwise the page is reloaded automatically (because that's what a submit event on a form does, apparently.) Now, suppose instead the template contains

<ComponentForDoingSomething @doThis={{someAction}}></ComponentForDoingSomething>

then we don't need a e.preventDefault() call... See how that's confusing? Sidenote: in reality the underlying ComponentForDoingSomething generally still performs a call to e.preventDefault, but that's none of our business, since principles like dependency inversion dictate that we do not concern ourselves with the implementation details of a child component, but only with the parts we can 'see' of that child component.

Now let's make the point even better: if we have a problematic template that contains both of these types of usages:

<form {{on 'submit' someAction}}></form>
<ComponentForDoingSomething @doThis={{someAction}}></ComponentForDoingSomething>

then what do we do? We need to call e.preventDefault() for one element in the template, but not for the other. But that's easily solved with the new helper:

<form {{on 'submit' (prevent-default someAction)}}></form>
<ComponentForDoingSomething @doThis={{someAction}}></ComponentForDoingSomething>

@DrumsnChocolate DrumsnChocolate marked this pull request as draft November 3, 2024 22:56
Copy link

codecov bot commented Nov 3, 2024

Codecov Report

Attention: Patch coverage is 4.87805% with 39 lines in your changes missing coverage. Please review.

Project coverage is 12.95%. Comparing base (45d3aac) to head (6971ca0).

Files with missing lines Patch % Lines
app/components/form/response/response-form.js 7.14% 13 Missing ⚠️
app/components/form/response/response-card.js 0.00% 9 Missing ⚠️
app/components/form/responses-table-card.js 0.00% 8 Missing ⚠️
app/components/form/form-form.js 16.66% 5 Missing ⚠️
app/helpers/prevent-default.js 0.00% 3 Missing ⚠️
app/components/form/response/open-question.js 0.00% 1 Missing ⚠️
Additional details and impacted files
@@                       Coverage Diff                        @@
##           refactor/action_helper_usage     #896      +/-   ##
================================================================
- Coverage                         13.24%   12.95%   -0.30%     
================================================================
  Files                               455      456       +1     
  Lines                              3194     3189       -5     
================================================================
- Hits                                423      413      -10     
- Misses                             2771     2776       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

DrumsnChocolate added a commit that referenced this pull request Nov 5, 2024
@DrumsnChocolate DrumsnChocolate force-pushed the refactor/action_helper_usage_electric_boogaloo branch from 8ab55a1 to 4ed2b17 Compare November 5, 2024 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant