Closed omairrazam closed 3 years ago
You should be able to use the server side callbacks as if the component is a reflex.
The location of the controller shouldn't matter for the frontend. It just assumes that the component name matches the name of the controller.
Are you running into any problems in particular?
@joshleblanc Thanks for looking at my issue.
I tried to consider Component as a reflex and used standard way to call hook, but it didn't work. I will try again. if you have any working example that would be helpful.
secondly my controller is called as data-controller="example-component--example-component"
if it is located inside my component but I see this is what is applied to my html from view_component_reflex data-controller="example"
which means it is pointing to controller outside of component directory at a common place.
Really appreciate your help.
@omairrazam I gave it a whirl on my end, and a component that looked like
# app/components/example_component/example_component.rb
class ExampleComponent::ExampleComponent < ViewComponentReflex::Component
end
Output the data controller example-component--example
. What does your file structure look like?
@joshleblanc really appreciate your help. Please see attached image. Thanks.
@joshleblanc i generated my component using view_component gem's command and then manually changed parent class of component to what your gem suggests.
rails generate component Example title --sidecar
Right now, there's no good way to generate the controller from the component, since it's not actually in the sidecar folder itself.
But you can use component_controller data: { controller: "example-component--example-component", key: key }
to override view component reflex's defaults.
@joshleblanc overriding did work to pick my controller but my reflex stopped working, here is some clue from console. please see snapshot.
@joshleblanc ok by reading the code of gem, I am able to spot what to override and it worked. I just override this class method
def self.stimulus_controller 'example-component--example-component' end
The only issue now I need help with is to utilize before and after reflex hooks. I tried to use it as flex as mentioned in this doc. https://docs.stimulusreflex.com/rtfm/lifecycle
But it didn't work.
@joshleblanc
This is good stuff. Thanks for looking into this.
I'll take a look at the callbacks
@omairrazam So I tested this in the expo, and this worked as expected:
class CounterComponent < ViewComponentReflex::Component
after_reflex do
p "Done" # This prints fine
end
def initialize
@count = 0
end
def increment
@count += 1
end
end
Tried it using method references as well, which worked.
I did find a problem, though: Changing callbacks at runtime would not register. You would need to restart the server entirely. I imagine this is the problem you were running into.
I've just pushed 3.1.14.pre5 which should re-wire the callbacks on request in development.
I've also updated the docs to include your fix for overriding the self.stimulus_controller
method.
Let me know if you run into anything else!
@joshleblanc thanks. yes it worked after restarting the server
https://docs.stimulusreflex.com/rtfm/lifecycle#server-side-reflex-callbacks https://docs.stimulusreflex.com/rtfm/lifecycle#custom-life-cycle-methods
Also view_component_reflex doesn't use sidecar stimulus controller but points to controller inside javascripts folder. How to manage that?