Share
Embed
Transcript
35 slides
-
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 -
–Michael Crismali, circa just now “There are six degrees of
JavaScript on Rails.” -
JavaScript is inescapable When it comes right down to it,
certain user interactions can only be done with JavaScript -
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 -
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 -
–Michael Crismali and people who read the talk description “The
previous slide contained a false choice.” -
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 -
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..." } -
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 -
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 -
<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") } } -
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 -
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) }) -
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 -
<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')) -
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 -
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 -
The Degrees Unobtrusive JavaScript jQuery and/or Widgets Structure and Stimulus
A Sprinkling of a Framework Framework Pages Single Page App Inside of Rails -
–The Text Above “You don’t have to choose just one
of the six degrees.” -
Mix and Match Responsibly There are some risks to mixing
the different degrees together, but it’s possible to do them well.