Coder Social home page Coder Social logo

Comments (2)

pablo-co avatar pablo-co commented on May 16, 2024

Is there any status on this? Happy to help if any help is needed.

from stimulus_reflex.

leastbad avatar leastbad commented on May 16, 2024

Hi @pablo-co! We saw you join our Discord but you didn't seem to stick around long enough for us to connect there. We're a friendly bunch and I hope you drop by.

There is definitely progress, but it's from a different direction than you might be expecting. While I can't say conclusively, it's highly unlikely that this functionality will be added to StimulusReflex, because it's simply outside of the mandate of that project.

The great news is that SR is built atop CableReady (same author) which makes it relatively easy to pull off what you want to do. I'm going to be writing a blog post covering it, but here's the basics:

First, question whether what you really want is a redirection. Being on a page and then suddenly being on another page is a deeply jarring experience for a typical user. It's much more likely that you want to either refresh the page or update some element on it. CableReady gives you several options for doing this.

There are some places in my project where I want to force a refresh of the current page. On the server side, depending on whether my Channel class is a stream_for or stream_from, I use:

ProposalChannel.broadcast_to(Proposal.find(29),{}) (for)
or
ActionCable.server.broadcast("notification_1", {}) (from)

To generate an empty payload. And then in my Stimulus controller on the client:

import { Controller } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'
import consumer from '../lib/consumer'
export default class extends Controller {
  connect () {
    this.element[this.identifier] = this
    StimulusReflex.register(this)
    if (this.element.dataset.id) {
      const controller = this
      consumer.subscriptions.create(
        {
          channel: 'ProposalChannel',
          id: this.element.dataset.id
        },
        {
          received () {
            controller.stimulate('ApplicationReflex#refresh')
          }
        }
      )
    }
  }
}

The ApplicationReflex itself is literally a no-op eg:

def refresh
  # do nothing
end

This forces the client to refresh the current page as it would appear if the user hit Refresh. It's not as sexy as a built-in redirect (you could replace the controller.stimulate() call with a Turbolinks.visit() or even window.location) but it does what you need.

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.