You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #1418 [Live] Rename data-action-name to use standard Stimulus features (weaverryan)
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
[Live] Rename data-action-name to use standard Stimulus features
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| Issues | Fix#1283Fix#1065
| License | MIT
Hi!
The `data-action-name` was flawed because the parser inside couldn't handle things like `,` or `)` - e.g. `data-action-name="save(I am a string with ) inside of it)"`
One solution was to write a giant parser to parse these arguments correctly. Instead, in a live stream, some of us decided to "just use Stimulus":
```diff
<button
data-action="live#action"
- data-action-name="debounce(300)|save"
+ data-live-action-param="debounce(300)|save"
>Save</button>
```
This uses [Stimulus action parameters](https://stimulus.hotwired.dev/reference/actions#action-parameters). The new syntax is basically the same length as the previous one, but it's pure Stimulus. Passing arguments is also slightly different, as they are a query string (this allows us to have special characters in the argument values without inventing our own syntax). For single arguments, things look like before (other than the new attribute name):
```diff
<button
data-action="live#action"
- data-action-name="addItem(id={{ item.id }}, itemName=CustomItem)"
+ data-live-action-param="addItem"
+ data-live-id-param="{{ item.id }}"
>Add Item</button>
```
This also removes the `|prevent` modifier... because... again ❗ Stimulus already does this:
```diff
<button
- data-action="live#action
+ data-action="live#action:prevent"
- data-action-name="prevent|save"
+ data-live-action-param="save"
>Save</button>
```
Cheers!
Commits
-------
98b0ea4 [Live] Rename data-action-name to use standard Stimulus features
thrownewError(`No action name provided on element: ${getElementAsTagText(event.currentTarget)}. Did you forget to add the "data-live-action-param" attribute?`);
2919
+
}
2920
+
constrawAction=params.action;
2921
+
constactionArgs=Object.assign({},params);
2922
+
deleteactionArgs.action;
2946
2923
constdirectives=parseDirectives(rawAction);
2947
2924
letdebounce=false;
2948
2925
directives.forEach((directive)=>{
2949
2926
letpendingFiles={};
2950
2927
constvalidModifiers=newMap();
2951
-
validModifiers.set('prevent',()=>{
2952
-
event.preventDefault();
2953
-
});
2954
2928
validModifiers.set('stop',()=>{
2955
2929
event.stopPropagation();
2956
2930
});
@@ -2985,12 +2959,15 @@ class LiveControllerDefault extends Controller {
@@ -3019,11 +2993,13 @@ class LiveControllerDefault extends Controller {
3019
2993
this.component.fingerprint=this.fingerprintValue;
3020
2994
}
3021
2995
getEmitDirectives(event){
3022
-
constelement=event.currentTarget;
3023
-
if(!element.dataset.event){
3024
-
thrownewError(`No data-event attribute found on element: ${getElementAsTagText(element)}`);
2996
+
constparams=event.params;
2997
+
if(!params.event){
2998
+
thrownewError(`No event name provided on element: ${getElementAsTagText(event.currentTarget)}. Did you forget to add the "data-live-event-param" attribute?`);
3025
2999
}
3026
-
consteventInfo=element.dataset.event;
3000
+
consteventInfo=params.event;
3001
+
consteventArgs=Object.assign({},params);
3002
+
deleteeventArgs.event;
3027
3003
constdirectives=parseDirectives(eventInfo);
3028
3004
constemits=[];
3029
3005
directives.forEach((directive)=>{
@@ -3039,7 +3015,7 @@ class LiveControllerDefault extends Controller {
0 commit comments