joshleblanc / view_component_reflex

Call component methods right from your markup
http://view-component-reflex-expo.grep.sh/
MIT License
291 stars 27 forks source link

How to use Stimulus Reflex Lifecycle Methods as per links in description using view_component_reflex? #65

Closed omairrazam closed 3 years ago

omairrazam commented 3 years ago

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?

joshleblanc commented 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?

omairrazam commented 3 years ago

@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.

joshleblanc commented 3 years ago

@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?

omairrazam commented 3 years ago
Screenshot 2021-09-10 at 1 08 20 AM

@joshleblanc really appreciate your help. Please see attached image. Thanks.

omairrazam commented 3 years ago

@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

joshleblanc commented 3 years ago

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.

omairrazam commented 3 years ago

@joshleblanc overriding did work to pick my controller but my reflex stopped working, here is some clue from console. please see snapshot.

Screenshot 2021-09-18 at 5 46 50 AM
omairrazam commented 3 years ago

@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

omairrazam commented 3 years ago

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

joshleblanc commented 3 years ago

This is good stuff. Thanks for looking into this.

I'll take a look at the callbacks

joshleblanc commented 3 years ago

@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!

omairrazam commented 3 years ago

@joshleblanc thanks. yes it worked after restarting the server