Closed jonathan-s closed 3 years ago
The only way reflex-specific lifecycle methods are intended to work is based on you attaching a data-controller="foo"
to the element.
What's of concern to me is the element.reflexData
not containing data part. Are you saying it's undefined?
Can you please prepare an MVCE using https://github.com/leastbad/stimulus_reflex_harness so we can see what you're seeing?
I was unable to replicate that in the rails application above. However, I've made some progress in terms of understanding what's happening. If someone would like to take a look I have a django app where I can replicate this.
We always have the data here:
https://github.com/hopsoft/stimulus_reflex/blob/master/javascript/stimulus_reflex.js#L103
Then at a later point we sometimes are not able to access element.reflexData
right here:
https://github.com/hopsoft/stimulus_reflex/blob/master/javascript/stimulus_reflex.js#L496
@marcoroth Since you changed this line here: https://github.com/hopsoft/stimulus_reflex/blob/6786181658ee3504fe19a5c496e1c2398a74368b/javascript/lifecycle.js#L18
Is there any reason why we don't do !reflexes[reflexId]
instead?
If element.reflexData
is empty as in my case (which I frankly still don't understand why that is), it would be possible to fill in the reflex data again on the next line with
element.reflexData = reflexes[reflexId].promise.data
Is this still a thing, @jonathan-s ?
@leastbad No longer a thing so closing this. I mentioned the following in the PR, do you have any thoughts around that?
On a related note, I still think it would be nice to not need to mention data-controller. Another idea for this would be to have it working by naming convention, if you have a CalendarReflex it would try to find a calendar as a controller and execute the lifecycle methods there.
This has come up a few times. I understand why you suggest it.
It's one thing to put our own stimulus-reflex
controller on an element. I think it's a step further / too far to assume that we want to put their controller on the element.
If we'd started this early on, we might well have ended up with a different approach.
One thing you could propose is the idea of a naming scheme where if you specify data-reflex="foo" that it might search for and potentially attach a foo_reflex_controller.js controller. That actually seems pretty reasonable, but that's no guarantee @hopsoft and the others would agree.
Bug Report
This issue is half feature proposal, half bug report.
Describe the bug
Assume that we have the following code, and when clicking on the element we issue a full page morph.
When you click on that element the standard stimulus reflex controller will pass everything correctly to the backend. However none of the lifecycle methods will work.
To get the
beforeNewCalendarEvent
working correctly you would need to add `data-controller="calendar" which points to the controller that contains the method.If have the data-controller included
afterNewCalendarEvent
will work the first time, but will stop working if you continue clicking. The reason for this is thatelement.reflexData
for some reason no longer contains any data.The proposal is that you should never need to include
data-controller
for lifecycle methods to work. If that is a must and you for some reason forgot to includedata-controller
lifecycle methods will fail silently which is no bueno.Expected / desired behavior
Given that we know the method we are looking for (in this case
afterNewCalendarEvent
as an example. We should be able to find that method among the registered controllers and then execute that.Assuming that there are two methods with the same name we would issue a warning that the lifecycle method won't be executed because we couldn't identify it properly.
(which may be problematic if you work with inheritance).
Additional info
This is the data that is being sent to front-end.
Versions
StimulusReflex
External tools
Django
Browser
Firefox / Chorme.