Comments (3)
I like the idea of supporting something like this in CableReady:
cable_ready[stream_name].refresh
That is smart enough to perform any of the following based on detected support.
- Trigger an implicit default StimulusReflex to morph the page and pick up latest changes
- Turbolinks visit to reload the page
- Reload the page via window.location
from stimulus_reflex.
Wow, that would be cool.
from stimulus_reflex.
Circling back on this, it seems like the pattern that works best is to use CableReady to broadcast a message to the client. This channel handler then initiates a SR stimulate() operation, refreshing the content. I've found that a good strategy is to create a Stimulus controller that is also an ActionCable channel consumer. Indeed, I don't actually have a channels folder as all of my channels are managed by Stimulus controllers, which can easily call StimulusReflex.
By way of example, here is a complete polymorphic comment system implemented via SR and CableReady:
class CommentChannel < ApplicationCable::Channel
def subscribed
stream_for params[:commentable_type].constantize.find(params[:commentable_id])
end
end
import { Controller } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'
import consumer from '../lib/consumer'
export default class extends Controller {
static targets = ['comment']
connect () {
this.element[this.identifier] = this
StimulusReflex.register(this)
if (this.element.dataset.commentableId) {
const controller = this
consumer.subscriptions.create(
{
channel: 'CommentChannel',
commentable_type: this.element.dataset.commentableType,
commentable_id: this.element.dataset.commentableId
},
{
received () {
controller.stimulate('ApplicationReflex#refresh')
}
}
)
}
}
comment (e) {
e.preventDefault()
if (this.commentTarget.value.length)
this.stimulate('CommentsReflex#comment', {
class: this.element.dataset.commentableType,
id: this.element.dataset.commentableId,
body: this.commentTarget.value
})
this.commentTarget.value = ''
}
}
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true, optional: true
belongs_to :user
after_save do
CommentChannel.broadcast_to(commentable, {})
end
end
class CommentsReflex < StimulusReflex::Reflex
delegate :current_user, to: :connection
def comment(payload)
current_user.comments.create({
body: payload["body"],
commentable_type: payload["class"],
commentable_id: payload["id"],
})
end
end
The after_save callback in the Events model could just as easily be an ActiveJob perform
method.
Also, the ApplicationReflex#refresh is an empty method that simply forces a content refresh.
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.