Coder Social home page Coder Social logo

js-routes's Introduction

JsRoutes

Generates javascript file that defines all Rails named routes as javascript helpers

Intallation

Your Rails Gemfile:

gem "js-routes"

Basic Setup (Asset Pipeline)

Require js routes file in application.js or other bundle

/*
= require js-routes
*/

Optional: If you need to customize routes file create initializer, like config/initializers/jsroutes.rb:

JsRoutes.setup do |config|
  config.option = value
end

Available options:

  • default_format - Format to append to urls. Default: blank
  • exclude - Array of regexps to exclude from js routes. Default: []
    • Note that regexp applied to named route not to URL
  • include - Array of regexps to include in js routes. Default: []
    • Note that regexp applied to named route not to URL
  • namespace - global object used to access routes. Default: Routes
    • Supports nested namespace like MyProject.routes
  • prefix - String representing a url path to prepend to all paths. Default: blank
    • Example: http://yourdomain.com. This will cause route helpers to generate full path only.

Advanced Setup

You can generate routes files on the application side like this:

JsRoutes.generate!("#{path}/app_routes.js", :namespace => "AppRoutes", :exclude => /^admin_/, :default_format => "json")
JsRoutes.generate!("#{path}/adm_routes.js", :namespace => "AdmRoutes", :include => /^admin_/, :default_format => "json")

In order to generate javascript to string and manipulate them yourself use: Like:

routes_js = JsRoutes.generate(options)

Usage

Configuration above will create a nice javascript file with Routes object that has all the rails routes available:

Routes.users_path() // => "/users"
Routes.user_path(1) // => "/users/1"
Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
Routes.new_user_project_path(1, {format: 'json'}) // => "/users/1/projects/new.json"
Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"
Routes.user_project_path(1,2, {hello: ['world', 'mars']}) // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"

Using serialized object as route function arguments:

var google = {id: 1, name: "Google"};
Routes.company_path(google) // => "/companies/1"
var google = {id: 1, name: "Google", to_param: "google"};
Routes.company_path(google) // => "/companies/google"

In order to make routes helpers available globally:

jQuery.extend(window, Routes)

What about security?

js-routes itself do not have security holes. It makes URLs without access protection more reachable by potential attacker. In order to prevent this use :exclude option for sensitive urls like /admin_/

Spork

When using Spork and Spork.trap_method(Rails::Application::RoutesReloader, :reload!) you should also do:

Spork.trap_method(JsRoutes, :generate!)

Advantages over alternatives

There are some alternatives available. Most of them has only basic feature and don't reach the level of quality I accept. Advantages of this one are:

  • Rails3 support
  • Rich options set
  • Support Rails #to_param convention for seo optimized paths
  • Well tested

Thanks to Contributors

Have fun

js-routes's People

Contributors

bogdan avatar mjtko avatar ehelms avatar le0pard avatar dougalcorn avatar rspace avatar fb3 avatar rosenfeld avatar teamon avatar levraipixel avatar aaronjensen avatar alesya-h avatar alexfarrill avatar haugstrup avatar biesiad avatar kl-7 avatar manuelmeurer avatar fletcherm avatar baconpat avatar legendetm avatar

Stargazers

张利 avatar

Watchers

James Cloos avatar 张利 avatar

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.