Giter Site home page Giter Site logo

Comments (9)

leastbad avatar leastbad commented on May 22, 2024 1

Alright, @benbonnet! I just made a PR on your project with fixes for all of your issues. Please let us know how you're doing and build cool stuff with StimulusReflex.

from stimulus_reflex.

leastbad avatar leastbad commented on May 22, 2024

Hey @benbonnet! Thanks for the question.

Do you have a repo you could share? I'd be happy to look at your code if that helps.

To my eyes and without seeing your reflex/controllers, data-reflex-root=".btn,.output" should work for the above example. The key detail is that you have to make sure that each selector resolves to one element; .btn will not work the way you want if you have multiple buttons.

In the second half of your question, I can say that it is possible and without any extra JS. Persistence is definitely required, but in this case, persistence can be the Rails session object.

Now, if you want to use individual scoping for each button, I would consider separating your design concerns from your StimulusReflex concerns. Here's my take:

<a href="#" class='btn count1' data-count="<%= @count1.to_i %>" data-step="1" data-reflex="click->ExampleReflex#counter1">
  <%= @count1.to_i %>
</a>

<a href="#" class="btn count2" data-count="<%= @count2.to_i %>" data-step="1" data-reflex="click->ExampleReflex#counter2">
  <%= @count2.to_i %>
</a>

I personally like using attributes for this sort of thing:

<div data-controller="example" data-reflex-root="[count1],[count2]">
<a href="#" class='btn' count1 data-count="<%= @count1.to_i %>" data-step="1" data-reflex="click->ExampleReflex#counter1">
  <%= @count1.to_i %>
</a>

<a href="#" class="btn" count2 data-count="<%= @count2.to_i %>" data-step="1" data-reflex="click->ExampleReflex#counter2">
  <%= @count2.to_i %>
</a>
</div>

Some people are allergic to arbitrarily defining their own attributes. Each to their own. I've been doing it with reckless abandon since 2004 and the sky has never fallen in the ways people predicted.

from stimulus_reflex.

leastbad avatar leastbad commented on May 22, 2024

Just as a footnote, there's something you could do, but I won't give an example because it's exactly what you should not actually do - and that is that you could pack all of your variables into every element. If you modified your code so that each button had data-count1 and data-count2 then you could technically get it working without any need for "persistence". But this is brittle, unworkable, ugly and you'd have to shoot yourself. Don't do this.

from stimulus_reflex.

benbonnet avatar benbonnet commented on May 22, 2024

@leastbad hello thx for your reply; I'll try this all out. I've shared a private one with you (for some reasons i'd prefer to keep it that way); it is quite simple yet, just trying out an approach that i believe should get more attention. no problems with using data-attributes. I haven't introduced persistence but I guess there might already be some stuff to understand just before this part

btw, the example repo you provide does not showcases any app/reflexes, while it surely is what many would be interested in

from stimulus_reflex.

leastbad avatar leastbad commented on May 22, 2024

Where did you get the link for that repo? It predates StimulusReflex.

You might be excited to see this.

I'll clone your repo now.

from stimulus_reflex.

leastbad avatar leastbad commented on May 22, 2024

Hey Ben,

I've figured out what's going on and it seems like you've uncovered a subtle bug. We're working on how to fix the issue but fixing your problem is mercifully easy. All you have to do is create app/javascript/controllers/example_controller.js with the following in it:

import ApplicationController from './application_controller'

export default class extends ApplicationController {}

I do have one question for you: when I cloned your repo, application_controller.js was in app/javascript instead of app/javascript/controllers where it is supposed to live. I moved it in my local copy; can you tell me whether you moved it there (and why) or if that's where the installer/rake task put it?

from stimulus_reflex.

benbonnet avatar benbonnet commented on May 22, 2024

Hey;

Thanks for your answer and the link above to the "real" repo ! I did moved application_controller.js manually, yes - I wanted to see what was the very minimal requirement and forgot to put it back to give it a go. Now I know, thx; i'll retry it soon.

Besides that, from what I can say, the generator process happened very smoothly

from stimulus_reflex.

leastbad avatar leastbad commented on May 22, 2024

I'm glad! We're still working out some of the kinks.

As for this issue with the missing example_controller.js... I'm trying to figure it out on my end. It might not be as quick a fix, and I'm trying to figure out why.

from stimulus_reflex.

leastbad avatar leastbad commented on May 22, 2024

One definite issue with your codebase is that somehow the index.js got seriously broken along the way. It's broken enough that we can't figure out how your app was doing anything at all, and in a funny way that's actually a bigger problem. Anyhow, here is what your index.js should look like unless you have a damn good reason to change it:

// Load all the controllers within this directory and all subdirectories.
// Controller files must be named *_controller.js.

import { Application } from 'stimulus'
import { definitionsFromContext } from 'stimulus/webpack-helpers'
import StimulusReflex from 'stimulus_reflex'

const application = Application.start()
const context = require.context('controllers', true, /_controller\.js$/)
application.load(definitionsFromContext(context))
StimulusReflex.initialize(application)

Still working on your specific issue, but for now feel free to update your source with the above.

from stimulus_reflex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.