Code Monkey home page Code Monkey logo

rails-javascript-integrations's Introduction

Rails JavaScript Integration Strategies

NOTE: This is more up-to-date than the last commit date would indicate, as most of the interesting work is not on the master branch.

Why this?

Componentized views make UI development orders of magnitude simpler. Unfortunately, scaling UI development and integrating a modern JavaScript workflow into a new or existing Rails app can be tricky.

What is this?

This project builds a simple todo list component with 4 different JavaScript integration strategies:

  • scoped-jquery: I wouldn't actually recommend this for anyone. But, if your team is currently using jQuery and adopting a new framework is a tough sell right now, here's a relatively simple pattern that may help.
  • vue-simple: A fantastic strategy for existing projects, as Vue plays well with jQuery and other DOM-manipulating libraries. This integration strategy also requires less than 1 minute of setup! As for learning curve, becoming very productive in Vue takes less than a day.
  • vue-browserify: Recommended strategy for most new projects. It takes about a half hour to set up, offers a UMD module system, very nice workflow with hot module reloading, and best of all - the configuration is simple enough that it's actually possible for mere mortals to understand everything that's happening!
  • vue-webpack: Recommended for people who want to be on the cutting edge and are willing to dive into the guts of build tools. It offers the best possible workflow and tooling possible for any framework at the time of writing.

How do I use this?

There are two recommended ways of browsing this project. The first is to read the diffs between branches in order of rising complexity. The current branch (master) is just a newly generated Rails app.

  1. master..scoped-jquery: Adds the todo model and API, with a single JavaScript file.
  2. scoped-jquery..vue-simple: Converts the scoped jQuery to a much simpler Vue component.
  3. vue-simple..vue-browserify: Moves all JavaScript to a new frontend folder managed by Browserify. Components are also organized into more advanced components in .vue files.
  4. vue-browserify..vue-webpack: Bypasses sprockets and all view-related Rails features, now managing the entire UI in the frontend folder through Webpack.

If there's a specific setup you're interested in and you want to see all work done from the point of a newly generated Rails app, just diff with master:

Contributing

If you notice a mistake, room for improvement, or have questions, issues and pull requests are very welcome. ๐Ÿ˜„

rails-javascript-integrations's People

Contributors

chrisvfritz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-javascript-integrations's Issues

How does the webpack integration work once deployed?

Hi @chrisvfritz, thanks for this repo, really useful!

I was looking at the vue-webpack solution and see it works by simply having webpack run alongside rails s and you've just put the directory "IN" the rails directory.

however... once you run npm run build where do the files go? And my real question is... once it's built and deployed to say Heroku, how does going to say "http://myvueapp.herokuapp.com" redirect to the built frontend files....?

thanks!

Trouble getting the browserify example to run

Hi there,

I stumbled across your excellent tutorials as I'm playing with vue.js at the moment and I want to port a rails site across.

Anyway, I'm thinking of going down the browserify route, as it seems the best one for me. So I cloned the vue-browserify branch. I then bundled, and ran the Procfile as per the README. Then I ran into a quite a few issues with the browserify Procfile command falling over.

To fix it, I ran:

npm install -g npm-run-all
npm install -g watchify
npm install -g http-server

And then the system was able to start up correctly, and I could post the TODO's etc. So it's all working now.

My question is, should I need to have run these install -g commands? If so, I'm happy to add them to the README in a pull request. If not, can you tell me what I was doing wrong?

Many many thanks for putting this together, it's the perfect starting point for me.

Oh, finally (and this is cheeky to put in an issue!) but, I can see that the system is compiling the js perfectly, and it looks like it is being served on localhost, but I can see in the logs that the http-serve says:

22:41:24 browserify.1 | > http-server -c 1 -a localhost
22:41:24 browserify.1 |
22:41:24 browserify.1 | Starting up http-server, serving ./public
22:41:24 browserify.1 | Available on:
22:41:24 browserify.1 |   http://localhost:5000
22:41:24 browserify.1 | Hit CTRL-C to stop the server
22:41:29 browserify.1 | 22:41:29 GMT+0200 (CEST) [HMR] Emitting updates
22:41:29 browserify.1 | 22:41:29 GMT+0200 (CEST) [HMR] Listening on localhost:3123
22:41:30 browserify.1 | 1625519 bytes written to app/assets/javascripts/application.js (5.18 seconds)
22:41:31 browserify.1 | 22:41:31 GMT+0200 (CEST) [HMR] User connected, syncing

can you explain what's going on there? When I look at localhost:5000 I get nothing. I don't really mind, as the app is perfect, I was just a little confused with it.

Anyway, the main thing is whether the installs needed to happen, feel free to ignore the server question :)

Thanks again - really appreciated this tutorial / quickstart!

Suggestion for other integration

Hi,

I just saw the description of the 'pure sprockets with jQuery' setup and while I'd recommend something else like React or Vue, etc. for front-end heavy work, I have created a cool (if I say so myself) plugin for exactly the situation where there is no room for anything but jQuery in a project (due to time, skills, necessity, etc).

Self plug:
https://github.com/abuisman/jquery-freud

What it allows you to do is componentize DOM elements by adding a data-attribute like so:

<div class='greeting-card' data-behaviours='IncreaseCount'></div>

The behaviour (class), would look like this:

class IncreaseCount
  constructor: (@element, options = {}) ->
    @count = @element.data('count') || 0
    $button = $('<button>Add one</button>')
    @element.append($button)
    $button.on('click', @handleButtonClick)

  buttonClick: =>
    @count = @count + 1;
    @element.find('.count').text(@count)

This way you can also selectively apply javascript to DOM elements as they come and go in and out of view while also being able to use classes and objects.

This approach has been very useful for a few years now for several projects where a full front-end framework is not a real option or simply overkill for various reasons.

Maybe you'd like to feature it as an alternative or share your thoughts about it here.

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.