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

Allow for default method for controller actions #756

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pythonandchips
Copy link

@pythonandchips pythonandchips commented Mar 16, 2024

Hi, I raised the possibility of allowing for devs to omit the method on an action and use a default method in issue #754 and put together a PR to see if there is any interest in this.

This allows for actions to omit the method and use _ as the method name on the controller. This will make it simpiler to use single method controllers.

For example if we have a controller that disables an element before we would write something like this:-

  <button data-controller="disable" data-action="click->disable#disable">Disable on click</button>
class DisableController extends Controller {
  disable({target}) {
    target.disabled = true
  }
}

After we can shorten the data-action attribute like this:-

  <button data-controller="disable" data-action="click->disable">Disable on click</button>
class DisableController extends Controller {
  _({target}) {
    target.disabled = true
  }
}

This makes it easier for developers to write the controller as they can give the controller a clear name and not have to construct a name for the method when its not needed.

This allows for actions to omit the method. When the method is omitted
stimulus will use `_` as the method name. This will make it simpiler to
use single method controllers.

For example if we have a controller that disables an element before we
would write something like this:-

```html
  <button data-action="click->disable#disable">Disable on click</button>
```

```js
class DisableController extends Controller {
  disable({target}) {
    target.disabled = true
  }
}
```

After we can shorten the `data-action` attribute like this:-

```html
  <button data-action="click->disable">Disable on click</button>
```

```js
class DisableController extends Controller {
  _({target}) {
    target.disabled = true
  }
}
```

This makes it easier for developers to write the controller as they can
give the controller a clear name and not have to construct a name for
the method when its not needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant