Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RailsConf - 6 Degrees of JavaScript on Rails

RailsConf - 6 Degrees of JavaScript on Rails

Michael Crismali

April 19, 2018
Tweet

Transcript

  1. Michael Crismali Senior developer at DevMynd Started as an apprentice

    at DevMynd Software A lot of work in Rails apps with a lot of JavaScript On Github as crismali
  2. JavaScript is inescapable When it comes right down to it,

    certain user interactions can only be done with JavaScript
  3. Teammates with differing skill levels A lot of teams write

    software and not everyone on the team is the same. People often have wildly different levels of comfort with JavaScript, especially when it’s not the primary language
  4. All in on JavaScript? As Rails developers, it can often

    feel like choosing between a classic server rendered HTML app or a single page JavaScript application with a Rails API
  5. –Michael Crismali and people who read the talk description “The

    previous slide contained a false choice.”
  6. What is it? Pretty slick Helps implement common UI patterns

    through Rails form helpers You don’t have to write any JavaScript So unobtrusive you may not know you’ve used it
  7. UJS by example link_to "Sign Out", destroy_users_session_path, method: :delete link_to

    "Dangerous zone", dangerous_zone_path, data: { confirm: 'Are you sure?' } f.submit data: { "disable-with": "Saving..." }
  8. jQuery and/or Widgets Classic! Probably not too organized Third party

    libraries are vendored or hosted from some CDN Examples: Typeahead search, custom dropdowns, making flash messages fade away
  9. Structure and Stimulus Hip hybrid Eventually we have enough JavaScript

    that we need to be organized Probably starting to lean on data attributes pretty hard Stimulus is one solution At this point we may be adding tests or trying to figure out other ways to pass data to our scripts
  10. <div data-controller="clipboard"> PIN: <input data-target="clipboard.source" type="text" value="1234" readonly> <button data-action="clipboard#copy">Copy

    to Clipboard</button> </div> // app/javascript/controllers/clipboard_controller.js import { Controller } from 'stimulus' export default class extends Controller { static targets = [ "source" ] connect() { if (document.queryCommandSupported("copy")) { this.element.classList.add("clipboard--supported") } } copy() { this.sourceTarget.select() document.execCommand("copy") } }
  11. A Sprinkling of a Framework Just a little taste For

    more complicated interactions, like wizards Leverage React/Angular/etc but just on a few pages Controls just part of the page
  12. class CommentBubble extends Component { state = { text: ''

    } handleSubmit = () => { saveComment(this.state.text).then(() => this.setState({ text: ''})) } render() { return <div className='comment-bubble'> <input placeholder='Your comment' type='text' value={this.state.text} onChange={(text) => this.setState({ text })} /> <button onClick={this.handleSubmit}>Submit</button> </div> } } document.getElementsByClassName('comment').forEach((element) => { ReactDOM.render(<CommentBubble />, element) })
  13. Framework Pages Some pages are mostly or entirely rendered by

    a JavaScript framework A lot of interaction Rails templates are pretty much just pack tags Possibly some client side routing
  14. <div id="wizard"></div> <%= stylesheet_pack_tag "sign-up-wizard" %> <%= javascript_pack_tag "sign-up-wizard" %>

    import React, { Component } from 'react' import ReactDOM from 'react-dom' import { Slides } from './Components' import { WizardRouter } from './WizardRouter' class SignUpWizard extends Component { render() { return <Slides type=''> <WizardRouter /> </Slide> } } ReactDOM.render(<SignUpWizard />, document.getElementById('wizard'))
  15. Single Page App(s) in Rails Basically a Rails API that

    happens to host a single page app Lots of client side routing Benefits of going all in on JavaScript without complicating your deploy processes
  16. Rails Routes and Template Rails.application.routes.draw do root to: "pages#app" get

    "/admin/*path", to: "pages#admin" get "/*path", to: "pages#app" end <div id="app-root"></div> <%= stylesheet_pack_tag "app" %> <%= javascript_pack_tag "app" %> <div id="admin-root"></div> <%= stylesheet_pack_tag "admin" %> <%= javascript_pack_tag “admin" %> routes.rb app.html.erb admin.html.erb
  17. The Degrees Unobtrusive JavaScript jQuery and/or Widgets Structure and Stimulus

    A Sprinkling of a Framework Framework Pages Single Page App Inside of Rails
  18. Mix and Match Responsibly There are some risks to mixing

    the different degrees together, but it’s possible to do them well.