Giter VIP home page Giter VIP logo

js-routes's Introduction

js:routes

js:routes is a simple rake task that gives you access to your Rails routes on the client side.

Features

  • Lightweight
  • Compatible with Rails 3.1.x
  • Generates vanilla JavaScript - no external library requirements
  • Supports setting custom filename

Explanation

js:routes is a single file you can place into your lib/tasks directory to give you access to the js:routes rake task. This task provides your rails-style routes on the client side.

# First, place the js.rake file in /lib/tasks
your_app/lib/tasks/js.rake

# You can generate your routes file by doing the following:
rake js:routes

# The above commands will place the routes in your_app/app/assets/javascripts/rails_routes.js
# You can specify your own filename like so:

rake js:routes[custom_name.js]
# The above commands will place the routes in your_app/app/assets/javascripts/custom_name.js

What does this change?

For example, let's say you have a NotesController with an update action. You have a post-it note style interface, and need to update the position of a note once the user stops dragging it around the screen. Your code might look something like this:

$.ajax({
    url: "/note/" + post_id;
    method: 'PUT',
    data:{
        x: post_x,
        y: post_y
    }
});

You'll notice the inelegant way that the route is being constructed. js:routes alleviates this by providing your Rails routes. The above code becomes much more elegant:

$.ajax({
    url: Paths.note({id: post_id})
    method: 'PUT',
    data:{
        x: post_x,
        y: post_y
    }
});

Paths can take an optional format parameter like: Paths.note({id: post_id, format: 'json'})

Hangups

  • The generated routes don't have anything to do with the associated HTTP Verb. You need to specify that yourself, as usual, and as demonstrated in the example above.
  • If a route is not available on the client side, please be sure you've re-run rake js:routes to regenerate the JavaScript routes file (It's not automatic!)
  • This project is not available as a plugin/gem. This is because it provides no application level functionality, and is very specific to the Rails environment. Simply clone the file into your lib/tasks and you're off to the races

Copyright and Licensing

Copyright (c) 2011 Mike Trpcic, released under the MIT license.

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.