Comments (9)
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.
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.
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.
@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.
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.
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.
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.
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.
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)
- Controller over reflex attribute is ignore and reflex is always called with default StimulusReflexController HOT 7
- Broken Next Page Link on Welcome Page of Docs HOT 2
- HEAD and most of the latest releases are broken, references an undefined exception
- StimulusReflex is not compatible with the standalone ActionCable server HOT 9
- Wrong dataset getting from Docs example HOT 2
- NameError: undefined local variable or method `package_json' HOT 3
- Reflex not triggered when a Stimulus controller is also attached to a field HOT 4
- Form parse errors are not captured and reported back to front-end
- Drop Rails 5 and 6.0 support
- `solid_cache` seems to work as well as Rails cache store HOT 1
- Add full-stack tests
- Stimulus_reflex and esbuild error HOT 1
- OpenStruct no longer supported by json HOT 1
- Refactor away instances of OpenStruct
- NameError: uninitialized constant StimulusReflex::Installer::Thor when running rake tasks HOT 1
- Support `relative_url_root`
- Could not find generator 'stimulus_reflex:initializer'
- Unrecognized command "esbuild" (Rails::Command::UnrecognizedCommandError)
- Installer should work in CI (noninteractive) environment
- docs recommend hiredis gem along with incompatible redis version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stimulus_reflex.