stimulus_reflex 3.5.0.pre8 → 3.5.0.pre10

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of stimulus_reflex might be problematic. Click here for more details.

Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -1218
  3. data/Gemfile +0 -1
  4. data/Gemfile.lock +145 -193
  5. data/README.md +48 -20
  6. data/Rakefile +0 -8
  7. data/app/assets/javascripts/stimulus_reflex.js +1174 -0
  8. data/app/assets/javascripts/stimulus_reflex.min.js +2 -0
  9. data/app/assets/javascripts/stimulus_reflex.min.js.map +1 -0
  10. data/app/assets/javascripts/stimulus_reflex.umd.js +1064 -0
  11. data/app/assets/javascripts/stimulus_reflex.umd.min.js +1065 -0
  12. data/app/assets/javascripts/stimulus_reflex.umd.min.js.map +1 -0
  13. data/app/channels/stimulus_reflex/channel.rb +28 -7
  14. data/bin/console +0 -2
  15. data/bin/standardize +2 -1
  16. data/lib/generators/stimulus_reflex/stimulus_reflex_generator.rb +72 -7
  17. data/lib/generators/stimulus_reflex/templates/app/controllers/examples_controller.rb.tt +9 -0
  18. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/consumer.js.tt +6 -0
  19. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.esbuild.tt +4 -0
  20. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.importmap.tt +2 -0
  21. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.shakapacker.tt +5 -0
  22. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.vite.tt +1 -0
  23. data/lib/generators/stimulus_reflex/templates/app/javascript/channels/index.js.webpacker.tt +5 -0
  24. data/lib/generators/stimulus_reflex/templates/app/javascript/config/cable_ready.js.tt +4 -0
  25. data/lib/generators/stimulus_reflex/templates/app/javascript/config/index.js.tt +2 -0
  26. data/lib/generators/stimulus_reflex/templates/app/javascript/config/mrujs.js.tt +9 -0
  27. data/lib/generators/stimulus_reflex/templates/app/javascript/config/stimulus_reflex.js.tt +5 -0
  28. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/%file_name%_controller.js.tt +114 -74
  29. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application.js.tt +11 -0
  30. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/application_controller.js.tt +49 -35
  31. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.esbuild.tt +7 -0
  32. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.importmap.tt +5 -0
  33. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.shakapacker.tt +5 -0
  34. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.vite.tt +5 -0
  35. data/lib/generators/stimulus_reflex/templates/app/javascript/controllers/index.js.webpacker.tt +5 -0
  36. data/lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt +38 -7
  37. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +10 -2
  38. data/lib/generators/stimulus_reflex/templates/app/views/examples/show.html.erb.tt +207 -0
  39. data/lib/generators/stimulus_reflex/templates/config/initializers/cable_ready.rb +22 -0
  40. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +18 -13
  41. data/lib/generators/stimulus_reflex/templates/esbuild.config.mjs.tt +94 -0
  42. data/lib/install/action_cable.rb +155 -0
  43. data/lib/install/broadcaster.rb +90 -0
  44. data/lib/install/bundle.rb +56 -0
  45. data/lib/install/compression.rb +41 -0
  46. data/lib/install/config.rb +87 -0
  47. data/lib/install/development.rb +110 -0
  48. data/lib/install/esbuild.rb +114 -0
  49. data/lib/install/example.rb +22 -0
  50. data/lib/install/importmap.rb +133 -0
  51. data/lib/install/initializers.rb +25 -0
  52. data/lib/install/mrujs.rb +133 -0
  53. data/lib/install/npm_packages.rb +25 -0
  54. data/lib/install/reflexes.rb +25 -0
  55. data/lib/install/shakapacker.rb +64 -0
  56. data/lib/install/spring.rb +54 -0
  57. data/lib/install/updatable.rb +34 -0
  58. data/lib/install/vite.rb +64 -0
  59. data/lib/install/webpacker.rb +90 -0
  60. data/lib/install/yarn.rb +55 -0
  61. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +15 -8
  62. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +7 -8
  63. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +10 -10
  64. data/lib/stimulus_reflex/broadcasters/update.rb +3 -0
  65. data/lib/stimulus_reflex/cable_readiness.rb +29 -0
  66. data/lib/stimulus_reflex/cable_ready_channels.rb +5 -5
  67. data/lib/stimulus_reflex/callbacks.rb +17 -1
  68. data/lib/stimulus_reflex/concern_enhancer.rb +6 -4
  69. data/lib/stimulus_reflex/configuration.rb +12 -2
  70. data/lib/stimulus_reflex/dataset.rb +11 -1
  71. data/lib/stimulus_reflex/engine.rb +40 -0
  72. data/lib/stimulus_reflex/html/document.rb +59 -0
  73. data/lib/stimulus_reflex/html/document_fragment.rb +13 -0
  74. data/lib/stimulus_reflex/importmap.rb +7 -0
  75. data/lib/stimulus_reflex/installer.rb +274 -0
  76. data/lib/stimulus_reflex/open_struct_fix.rb +31 -0
  77. data/lib/stimulus_reflex/reflex.rb +32 -25
  78. data/lib/stimulus_reflex/reflex_data.rb +18 -2
  79. data/lib/stimulus_reflex/reflex_factory.rb +6 -3
  80. data/lib/stimulus_reflex/request_parameters.rb +2 -0
  81. data/lib/stimulus_reflex/utils/logger.rb +12 -0
  82. data/lib/stimulus_reflex/utils/sanity_checker.rb +8 -106
  83. data/lib/stimulus_reflex/version.rb +1 -1
  84. data/lib/stimulus_reflex.rb +4 -6
  85. data/lib/tasks/stimulus_reflex/stimulus_reflex.rake +252 -0
  86. data/package.json +73 -0
  87. data/rollup.config.mjs +86 -0
  88. data/stimulus_reflex.gemspec +60 -0
  89. data/web-test-runner.config.mjs +12 -0
  90. data/yarn.lock +5098 -0
  91. metadata +191 -78
  92. data/LATEST +0 -1
  93. data/lib/generators/USAGE +0 -14
  94. data/lib/generators/stimulus_reflex/initializer_generator.rb +0 -14
  95. data/lib/tasks/stimulus_reflex/install.rake +0 -116
  96. data/test/broadcasters/broadcaster_test.rb +0 -11
  97. data/test/broadcasters/broadcaster_test_case.rb +0 -39
  98. data/test/broadcasters/nothing_broadcaster_test.rb +0 -30
  99. data/test/broadcasters/page_broadcaster_test.rb +0 -77
  100. data/test/broadcasters/selector_broadcaster_test.rb +0 -167
  101. data/test/callbacks_test.rb +0 -652
  102. data/test/concern_enhancer_test.rb +0 -54
  103. data/test/element_test.rb +0 -254
  104. data/test/generators/stimulus_reflex_generator_test.rb +0 -58
  105. data/test/reflex_test.rb +0 -43
  106. data/test/test_helper.rb +0 -71
  107. data/test/tmp/app/reflexes/application_reflex.rb +0 -19
  108. data/test/tmp/app/reflexes/demo_reflex.rb +0 -35
@@ -1,101 +1,141 @@
1
1
  import ApplicationController from './application_controller'
2
2
 
3
- /* This is the custom StimulusReflex controller for the <%= class_name %> Reflex.
4
- * Learn more at: https://docs.stimulusreflex.com
5
- */
6
- export default class extends ApplicationController {
7
- /*
8
- * Regular Stimulus lifecycle methods
9
- * Learn more at: https://stimulusjs.org/reference/lifecycle-callbacks
10
- *
11
- * If you intend to use this controller as a regular stimulus controller as well,
12
- * make sure any Stimulus lifecycle methods overridden in ApplicationController call super.
13
- *
14
- * Important:
15
- * By default, StimulusReflex overrides the -connect- method so make sure you
16
- * call super if you intend to do anything else when this controller connects.
17
- */
3
+ <%- if class_name == "Example" -%>
4
+ //
5
+ // This is the Stimulus controller for the Example Reflex.
6
+ //
7
+ // It corresponds to the server-side Reflex class located at /app/reflexes/example.rb
8
+ //
9
+ // Learn more at: https://docs.stimulusreflex.com/guide/reflexes
10
+ //
18
11
 
12
+ <%- end -%>
13
+ export default class extends ApplicationController {
19
14
  connect () {
15
+ <%- if class_name == "Example" -%>
16
+ //
17
+ // StimulusReflex overrides the Stimulus `connect` method. Make sure to call
18
+ // `super.connect()` so that any code in your superclass `connect` is run.
19
+ //
20
+ <%- end -%>
20
21
  super.connect()
21
- // add your code here, if applicable
22
22
  }
23
+ <%- if class_name == "Example" -%>
23
24
 
24
- /* Reflex specific lifecycle methods.
25
- *
26
- * For every method defined in your Reflex class, a matching set of lifecycle methods become available
27
- * in this javascript controller. These are optional, so feel free to delete these stubs if you don't
28
- * need them.
29
- *
30
- * Important:
31
- * Make sure to add data-controller="<%= class_name.underscore.dasherize %>" to your markup alongside
32
- * data-reflex="<%= class_name %>#dance" for the lifecycle methods to fire properly.
33
- *
34
- * Example:
35
- *
36
- * <a href="#" data-reflex="click-><%= class_name %>#dance" data-controller="<%= class_name.underscore.dasherize %>">Dance!</a>
37
- *
38
- * Arguments:
39
- *
40
- * element - the element that triggered the reflex
41
- * may be different than the Stimulus controller's this.element
42
- *
43
- * reflex - the name of the reflex e.g. "<%= class_name %>#dance"
44
- *
45
- * error/noop - the error message (for reflexError), otherwise null
46
- *
47
- * reflexId - a UUID4 or developer-provided unique identifier for each Reflex
48
- */
49
-
50
- <% if actions.empty? -%>
51
- // Assuming you create a "<%= class_name %>#dance" action in your Reflex class
52
- // you'll be able to use the following lifecycle methods:
53
-
54
- // beforeDance(element, reflex, noop, reflexId) {
55
- // element.innerText = 'Putting dance shoes on...'
25
+ // With StimulusReflex active in your project, it will continuously scan your DOM for
26
+ // `data-reflex` attributes on your elements, even if they are dynamically created.
27
+ //
28
+ // <a href="#" data-reflex="click->Example#dance">Dance!</a>
29
+ //
30
+ // We call this a "declared" Reflex, because it doesn't require any JS to run.
31
+ //
32
+ // When your user clicks this link, a Reflex is launched and it calls the `dance` method
33
+ // on your Example Reflex class. You don't have to do anything else!
34
+ //
35
+ // This Stimulus controller doesn't even need to exist for StimulusReflex to work its magic.
36
+ // https://docs.stimulusreflex.com/guide/reflexes#declaring-a-reflex-in-html-with-data-attributes
37
+ //
38
+ // However...
39
+ //
40
+ // If we do create an `example` Stimulus controller that extends `ApplicationController`,
41
+ // we can unlock several additional capabilities, including creating Reflexes with code.
42
+ //
43
+ // <a href="#" data-controller="example" data-action="example#dance">Dance!</a>
44
+ //
45
+ // StimulusReflex gives our controller a new method, `stimulate`:
46
+ //
47
+ // dance() {
48
+ // this.stimulate('Example#dance')
56
49
  // }
50
+ //
51
+ // The `stimulate` method is very flexible, and it gives you the opportunity to pass
52
+ // parameter options that will be passed to the `dance` method on the server.
53
+ // https://docs.stimulusreflex.com/guide/reflexes#calling-a-reflex-in-a-stimulus-controller
54
+ //
55
+ // Reflex lifecycle methods
56
+ //
57
+ // For every method defined in your Reflex class, a matching set of optional
58
+ // lifecycle callback methods become available in this javascript controller.
59
+ // https://docs.stimulusreflex.com/guide/lifecycle#client-side-reflex-callbacks
60
+ //
61
+ // <a href="#" data-reflex="click->Example#dance" data-controller="example">Dance!</a>
62
+ //
63
+ // StimulusReflex will check for the presence of several methods:
64
+ //
65
+ // afterReflex(element, reflex, noop, id) {
66
+ // // fires after every Example Reflex action
67
+ // }
68
+ //
69
+ // afterDance(element, reflex, noop, id) {
70
+ // // fires after Example Reflexes calling the dance action
71
+ // }
72
+ //
73
+ // Arguments:
74
+ //
75
+ // element - the element that triggered the reflex
76
+ // may be different than the Stimulus controller's this.element
77
+ //
78
+ // reflex - the name of the reflex e.g. "Example#dance"
79
+ //
80
+ // error/noop - the error message (for reflexError), otherwise null
81
+ //
82
+ // id - a UUID4 or developer-provided unique identifier for each Reflex
83
+ //
84
+ // Access to the client-side Reflex objects created by this controller
85
+ //
86
+ // Every Reflex you create is represented by an object in the global Reflexes collection.
87
+ // You can access the Example Reflexes created by this controller via the `reflexes` proxy.
88
+ //
89
+ // this.reflexes.last
90
+ // this.reflexes.all
91
+ // this.reflexes.all[id]
92
+ // this.reflexes.error
93
+ //
94
+ // The proxy allows you to access the most recent Reflex, an array of all Reflexes, a specific
95
+ // Reflex specified by its `id` and an array of all Reflexes in a given lifecycle stage.
96
+ //
97
+ // If you explore the Reflex object, you'll see all relevant details,
98
+ // including the `data` that is being delivered to the server.
99
+ //
100
+ // Pretty cool, right?
101
+ //
102
+ <%- end -%>
103
+ <%- actions.each do |action| -%>
57
104
 
58
- // danceSuccess(element, reflex, noop, reflexId) {
59
- // element.innerText = '\nDanced like no one was watching! Was someone watching?'
105
+ // <%= "before_#{action}".camelize(:lower) %>(element, reflex, noop, id) {
106
+ // console.log("before <%= action %>", element, reflex, id)
60
107
  // }
61
108
 
62
- // danceError(element, reflex, error, reflexId) {
63
- // console.error('danceError', error);
64
- // element.innerText = "\nCouldn\'t dance!"
109
+ // <%= "#{action}_queued".camelize(:lower) %>(element, reflex, noop, id) {
110
+ // console.log("<%= action %> enqueued for delivery upon connection", element, reflex, id)
65
111
  // }
66
112
 
67
- // afterDance(element, reflex, noop, reflexId) {
68
- // element.innerText = '\nWhatever that was, it\'s over now.'
113
+ // <%= "#{action}_delivered".camelize(:lower) %>(element, reflex, noop, id) {
114
+ // console.log("<%= action %> delivered to the server", element, reflex, id)
69
115
  // }
70
116
 
71
- // finalizeDance(element, reflex, noop, reflexId) {
72
- // element.innerText = '\nNow, the cleanup can begin!'
73
- // }
74
- <% end -%>
75
- <% actions.each do |action| -%>
76
- // <%= "before_#{action}".camelize(:lower) %>(element, reflex, noop, reflexId) {
77
- // console.log("before <%= action %>", element, reflex, reflexId)
117
+ // <%= "#{action}_success".camelize(:lower) %>(element, reflex, noop, id) {
118
+ // console.log("<%= action %> successfully executed", element, reflex, id)
78
119
  // }
79
120
 
80
- // <%= "#{action}_success".camelize(:lower) %>(element, reflex, noop, reflexId) {
81
- // console.log("<%= action %> success", element, reflex, reflexId)
121
+ // <%= "#{action}_error".camelize(:lower) %>(element, reflex, error, id) {
122
+ // console.error("<%= action %> server-side error", element, reflex, error, id)
82
123
  // }
83
124
 
84
- // <%= "#{action}_error".camelize(:lower) %>(element, reflex, error, reflexId) {
85
- // console.error("<%= action %> error", element, reflex, error, reflexId)
125
+ // <%= "#{action}_halted".camelize(:lower) %>(element, reflex, noop, id) {
126
+ // console.warn("<%= action %> halted before execution", element, reflex, id)
86
127
  // }
87
128
 
88
- // <%= "#{action}_halted".camelize(:lower) %>(element, reflex, noop, reflexId) {
89
- // console.warn("<%= action %> halted", element, reflex, reflexId)
129
+ // <%= "#{action}_forbidden".camelize(:lower) %>(element, reflex, noop, id) {
130
+ // console.warn("<%= action %> forbidden from executing", element, reflex, id)
90
131
  // }
91
132
 
92
- // <%= "after_#{action}".camelize(:lower) %>(element, reflex, noop, reflexId) {
93
- // console.log("after <%= action %>", element, reflex, reflexId)
133
+ // <%= "after_#{action}".camelize(:lower) %>(element, reflex, noop, id) {
134
+ // console.log("<%= action %> has been executed by the server", element, reflex, id)
94
135
  // }
95
136
 
96
- // <%= "finalize_#{action}".camelize(:lower) %>(element, reflex, noop, reflexId) {
97
- // console.log("finalize <%= action %>", element, reflex, reflexId)
137
+ // <%= "finalize_#{action}".camelize(:lower) %>(element, reflex, noop, id) {
138
+ // console.log("<%= action %> changes have been applied", element, reflex, id)
98
139
  // }
99
- <%= "\n" unless action == actions.last -%>
100
- <% end -%>
140
+ <%- end -%>
101
141
  }
@@ -0,0 +1,11 @@
1
+ import { Application } from "@hotwired/stimulus"
2
+ import consumer from "../channels/consumer"
3
+
4
+ const application = Application.start()
5
+
6
+ // Configure Stimulus development experience
7
+ application.debug = false
8
+ application.consumer = consumer
9
+ window.Stimulus = application
10
+
11
+ export { application }
@@ -1,60 +1,74 @@
1
- import { Controller } from 'stimulus'
1
+ import { Controller } from '@hotwired/stimulus'
2
2
  import StimulusReflex from 'stimulus_reflex'
3
3
 
4
- /* This is your ApplicationController.
5
- * All StimulusReflex controllers should inherit from this class.
6
- *
7
- * Example:
8
- *
9
- * import ApplicationController from './application_controller'
10
- *
11
- * export default class extends ApplicationController { ... }
12
- *
13
- * Learn more at: https://docs.stimulusreflex.com
14
- */
4
+ // This is the Stimulus ApplicationController.
5
+ // All StimulusReflex controllers should inherit from this class.
6
+ //
7
+ // Example:
8
+ //
9
+ // import ApplicationController from './application_controller'
10
+ //
11
+ // export default class extends ApplicationController { ... }
12
+ //
13
+ // Learn more at: https://docs.stimulusreflex.com
14
+ //
15
+
15
16
  export default class extends Controller {
16
17
  connect () {
17
18
  StimulusReflex.register(this)
18
19
  }
19
20
 
20
- /* Application-wide lifecycle methods
21
- *
22
- * Use these methods to handle lifecycle concerns for the entire application.
23
- * Using the lifecycle is optional, so feel free to delete these stubs if you don't need them.
24
- *
25
- * Arguments:
26
- *
27
- * element - the element that triggered the reflex
28
- * may be different than the Stimulus controller's this.element
29
- *
30
- * reflex - the name of the reflex e.g. "Example#demo"
31
- *
32
- * error/noop - the error message (for reflexError), otherwise null
33
- *
34
- * reflexId - a UUID4 or developer-provided unique identifier for each Reflex
35
- */
36
-
37
- beforeReflex (element, reflex, noop, reflexId) {
21
+ // Application-wide lifecycle methods
22
+ //
23
+ // Use these methods to handle lifecycle callbacks for all controllers.
24
+ // Using lifecycle methods is optional, so feel free to delete these if you don't need them.
25
+ //
26
+ // Arguments:
27
+ //
28
+ // element - the element that triggered the reflex
29
+ // may be different than the Stimulus controller's this.element
30
+ //
31
+ // reflex - the name of the reflex e.g. "Example#demo"
32
+ //
33
+ // error/noop - the error message (for reflexError), otherwise null
34
+ //
35
+ // id - a UUID4 or developer-provided unique identifier for each Reflex
36
+ //
37
+
38
+ beforeReflex (element, reflex, noop, id) {
38
39
  // document.body.classList.add('wait')
39
40
  }
40
41
 
41
- reflexSuccess (element, reflex, noop, reflexId) {
42
+ reflexQueued (element, reflex, noop, id) {
43
+ // Reflex will be delivered to server upon reconnection
44
+ }
45
+
46
+ reflexDelivered (element, reflex, noop, id) {
47
+ // Reflex has been delivered to the server
48
+ }
49
+
50
+ reflexSuccess (element, reflex, noop, id) {
42
51
  // show success message
43
52
  }
44
53
 
45
- reflexError (element, reflex, error, reflexId) {
54
+ reflexError (element, reflex, error, id) {
46
55
  // show error message
47
56
  }
48
57
 
49
- reflexHalted (element, reflex, error, reflexId) {
58
+ reflexForbidden (element, reflex, noop, id) {
59
+ // Reflex action did not have permission to run
60
+ // window.location = '/'
61
+ }
62
+
63
+ reflexHalted (element, reflex, noop, id) {
50
64
  // handle aborted Reflex action
51
65
  }
52
66
 
53
- afterReflex (element, reflex, noop, reflexId) {
67
+ afterReflex (element, reflex, noop, id) {
54
68
  // document.body.classList.remove('wait')
55
69
  }
56
70
 
57
- finalizeReflex (element, reflex, noop, reflexId) {
71
+ finalizeReflex (element, reflex, noop, id) {
58
72
  // all operations have completed, animation etc is now safe
59
73
  }
60
74
  }
@@ -0,0 +1,7 @@
1
+ import { application } from "./application"
2
+
3
+ import controllers from "./**/*_controller.js"
4
+
5
+ controllers.forEach((controller) => {
6
+ application.register(controller.name, controller.module.default)
7
+ })
@@ -0,0 +1,5 @@
1
+ import { application } from "./application"
2
+
3
+ // Eager load all controllers defined in the import map under controllers/**/*_controller
4
+ import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
5
+ eagerLoadControllersFrom("controllers", application)
@@ -0,0 +1,5 @@
1
+ import { application } from "./application"
2
+ import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers"
3
+
4
+ const controllers = definitionsFromContext(require.context("controllers", true, /_controller\.js$/))
5
+ application.load(controllers)
@@ -0,0 +1,5 @@
1
+ import { application } from "./application"
2
+ import { registerControllers } from "stimulus-vite-helpers"
3
+
4
+ const controllers = import.meta.globEager("./**/*_controller.js");
5
+ registerControllers(application, controllers)
@@ -0,0 +1,5 @@
1
+ import { application } from "./application"
2
+ import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers"
3
+
4
+ const controllers = definitionsFromContext(require.context("controllers", true, /_controller\.js$/))
5
+ application.load(controllers)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class <%= class_name %>Reflex < ApplicationReflex
4
+ <%- if class_name == "Example" -%>
4
5
  # Add Reflex methods in this file.
5
6
  #
6
7
  # All Reflex instances include CableReady::Broadcaster and expose the following properties:
@@ -16,26 +17,56 @@ class <%= class_name %>Reflex < ApplicationReflex
16
17
  # - signed - use a signed Global ID to map dataset attribute to a model eg. element.signed[:foo]
17
18
  # - unsigned - use an unsigned Global ID to map dataset attribute to a model eg. element.unsigned[:foo]
18
19
  # - cable_ready - a special cable_ready that can broadcast to the current visitor (no brackets needed)
19
- # - reflex_id - a UUIDv4 that uniquely identies each Reflex
20
+ # - id - a UUIDv4 that uniquely identifies each Reflex
20
21
  # - tab_id - a UUIDv4 that uniquely identifies the browser tab
21
22
  #
22
23
  # Example:
23
24
  #
24
25
  # before_reflex do
25
26
  # # throw :abort # this will prevent the Reflex from continuing
26
- # # learn more about callbacks at https://docs.stimulusreflex.com/rtfm/lifecycle
27
+ # # learn more about callbacks at https://docs.stimulusreflex.com/guide/lifecycle
27
28
  # end
28
29
  #
29
30
  # def example(argument=true)
30
- # # Your logic here...
31
+ # # Your logic here! Update models, launch jobs, poll Redis...
32
+ #
33
+ # # By default, Reflexes call the controller action for the current page and render the view.
31
34
  # # Any declared instance variables will be made available to the Rails controller and view.
35
+ #
36
+ # # You can also use the `morph` method to mark a Reflex as Selector or Nothing.
37
+ # # https://docs.stimulusreflex.com/guide/morph-modes#introducing-morphs
32
38
  # end
33
39
  #
34
- # Learn more at: https://docs.stimulusreflex.com/rtfm/reflex-classes
40
+ # Learn more at: https://docs.stimulusreflex.com/guide/reflex-classes
41
+
42
+ <%- end -%>
43
+ <%- if class_name == "Example" -%>
44
+ def increment
45
+ count = session[:count] || 0
46
+ session[:count] = count + 1
47
+
48
+ morph "#time", Time.now
49
+ morph "#count", session[:count]
50
+
51
+ cable_ready.set_value(selector: "progress", value: session[:count])
35
52
 
36
- <% actions.each do |action| -%>
53
+ if session[:count] == 5
54
+ cable_ready.text_content(selector: "#reload", text: "Try reloading your browser window. The count is persisted in the session!")
55
+ end
56
+
57
+ if session[:count] >= 10
58
+ cable_ready.dispatch_event(name: "fireworks")
59
+ end
60
+ end
61
+
62
+ def reset
63
+ session[:count] = 0
64
+ end
65
+ <%- else -%>
66
+ <%- actions.each do |action| -%>
37
67
  def <%= action %>
38
68
  end
39
- <%= "\n" unless action == actions.last -%>
40
- <% end -%>
69
+ <%= "\n" unless action == actions.last -%>
70
+ <%- end -%>
71
+ <%- end -%>
41
72
  end
@@ -3,11 +3,19 @@
3
3
  class ApplicationReflex < StimulusReflex::Reflex
4
4
  # Put application-wide Reflex behavior and callbacks in this file.
5
5
  #
6
- # Learn more at: https://docs.stimulusreflex.com/rtfm/reflex-classes
6
+ # Learn more at: https://docs.stimulusreflex.com/guide/reflex-classes
7
7
  #
8
8
  # If your ActionCable connection is: `identified_by :current_user`
9
9
  # delegate :current_user, to: :connection
10
10
  #
11
+ # current_user delegation alloqs you to use the Current pattern, too:
12
+ # before_reflex do
13
+ # Current.user = current_user
14
+ # end
15
+ #
16
+ # To access view helpers inside Reflexes:
17
+ # delegate :helpers, to: :ApplicationController
18
+ #
11
19
  # If you need to localize your Reflexes, you can set the I18n locale here:
12
20
  #
13
21
  # before_reflex do
@@ -15,5 +23,5 @@ class ApplicationReflex < StimulusReflex::Reflex
15
23
  # end
16
24
  #
17
25
  # For code examples, considerations and caveats, see:
18
- # https://docs.stimulusreflex.com/rtfm/patterns#internationalization
26
+ # https://docs.stimulusreflex.com/guide/patterns#internationalization
19
27
  end