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

How to pass arguments through actions? #64

Closed
bihnkim opened this issue Jan 19, 2018 · 12 comments
Closed

How to pass arguments through actions? #64

bihnkim opened this issue Jan 19, 2018 · 12 comments

Comments

@bihnkim
Copy link

bihnkim commented Jan 19, 2018

For the slideshow example:

If I wanted to "jump" to the second image and would like to implement a "jump" action, I need to pass in the index of the clicked image. How can this be done?

@savroff
Copy link

savroff commented Jan 19, 2018

@bihnkim

<div data-action="jump" data-slide-index="0">Open First slide</div>
jump(event) {
  const element = event.target
  this.targets.findAll('slides')[element.dataset.slideIndex]
  // next your logic to switch slides
}

@adrienpoly
Copy link
Member

You could also write it like this

jump(_event, element) {
  this.targets.findAll('slides')[element.dataset.slideIndex]
  // next your logic to switch slides
}```

@javan
Copy link
Contributor

javan commented Jan 19, 2018

Those are both reasonable options. Action methods are essentially just event handlers and there isn't a way to pass additional arguments to them.

@javan javan closed this as completed Jan 19, 2018
@askehansen
Copy link

Im having some issues with the example provided by @adrianholovaty.
If you have an element inside the action element, then _event.target returns the nested element instead in safari.
Also element is just undefined.

This jsbin shows the issues: http://jsbin.com/xiyuho/edit?html,js,console,output

@adrienpoly
Copy link
Member

adrienpoly commented Feb 28, 2018

@askehansen element as the second argument of your function was available in Stimulus 0.9 but this has been removed in 1.0. You must use now even.target

about safari if you look at the properties of event.targeton both browser they are the same

http://jsbin.com/nasopusali/1/edit?html,js,console,output

@javan
Copy link
Contributor

javan commented Feb 28, 2018

event.target returns the nested element

Use event.currentTarget to get the action element. Some related discussion here: https://discourse.stimulusjs.org/t/is-controller-scope-element-safe-to-use/109/9

@zorn-v
Copy link

zorn-v commented Mar 16, 2021

Use event.currentTarget to get the action element.

Stimulus is the crap then, no ?
Why not to introduce some other vars like "maybeThisIsWhatYouNeed" and "actuallyThisVarYouNeed" ?
Fuck standart vars.

@zorn-v
Copy link

zorn-v commented Mar 16, 2021

I'm just playing with something like data-action="click->invoices#print" data-invoice="{{ invoice.extIdString }}" and I was surprised that sometimes it print what I want (console.log), but sometimes (several F5) "undefined" with

  print(e) {
    console.log(e.target.dataset.invoice)
  }

After change "target" to "currentTarget" all fine. Is not is the crap ?

PS. Thanks to this topic

@javan
Copy link
Contributor

javan commented Mar 16, 2021

Stimulus is the crap then, no ?

No.

Event.currentTarget

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.target, which identifies the element on which the event occurred and which may be its descendant.

@zorn-v
Copy link

zorn-v commented Mar 16, 2021

Argument. Thanks, was not right.

@zorn-v
Copy link

zorn-v commented Mar 16, 2021

I have idea, all not understanding is about not passing DOM element to handler (I think it is common case).
Why not to pass it as second arg ?

@zorn-v
Copy link

zorn-v commented Mar 16, 2021

What I want - defined behavior of my code.

@hotwired hotwired locked as resolved and limited conversation to collaborators Mar 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

6 participants