Sha256: 1bc0abe0676c4d874cdfde23b2d200518e2b2e5188669a3beef880ba99513c72

Contents?: true

Size: 1015 Bytes

Versions: 4

Compression:

Stored size: 1015 Bytes

Contents

import { Controller } from '@hotwired/stimulus'
import CableReady from 'cable_ready'

export default class extends Controller {
  static values = { id: Number }

  connect () {
    if (this.preview) return
    if (this.application.consumer) {
      this.channel = this.application.consumer.subscriptions.create(
        {
          channel: '<%= class_name %>Channel',
          id: this.idValue
        },
        {
          received (data) {
            if (data.cableReady) CableReady.perform(data.operations)
          }
        }
      )
    } else {
      console.error(
        `The "<%= class_name.underscore.dasherize %>" Stimulus controller requires an Action Cable consumer.\nPlease set 'application.consumer = consumer' in your application.js.`
      )
    }
  }

  disconnect () {
    this.channel.unsubscribe()
  }

  get preview () {
    return (
      document.documentElement.hasAttribute('data-turbolinks-preview') ||
      document.documentElement.hasAttribute('data-turbo-preview')
    )
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cable_ready-5.0.3 lib/generators/cable_ready/templates/app/javascript/controllers/%file_name%_controller.js.tt
cable_ready-5.0.2 lib/generators/cable_ready/templates/app/javascript/controllers/%file_name%_controller.js.tt
cable_ready-5.0.1 lib/generators/cable_ready/templates/app/javascript/controllers/%file_name%_controller.js.tt
cable_ready-5.0.0 lib/generators/cable_ready/templates/app/javascript/controllers/%file_name%_controller.js.tt