Announcing:

LessConf 2012 will be Feb 23-24 in Atlanta, Georgia!
Register today and make us smile super big!

Named Routes in Javascript - LessJsRoutes updated

written by Allan on September 15, 2011

Less JS Routes has just been released with some exciting new updates.

It can be found at http://github.com/stevenbristol/less-js-routes

From the README file:

LessJsRoutes

Have you ever wanted to use named routes in your JavaScript? So have I. Now you can.

LessJsRoutes will generate a javascript file with functions that give you the path to the route and functions that will call these routes using ajax (uses jQuery or Prototype).

Features

  • Gives you a javascript function that will return the path for any route.
  • Gives you a javascript function that will call any route with the proper method (PUT/POST/etc).
  • Let's you control which routes get generated with :only, :ignore flags.
  • Handles nested routes.

Installation

NOTE for Rails 2 use branch "rails-2" NOTE: for Rails versions < 2.3 use version d9cacd51454814cf4268da007c439573418adbcd.

Add it to your Gemfile:

gem 'less-js-routes', :git => "http://github.com/stevenbristol/less-js-routes"

Generate the less_routes.js file

rake less:js:routes

or

Less::Js::Routes.generate!

This file should be regenerated any time the routes change, so it's a good idea to put it into your deploy script or run it any time your app starts.

Configuration Options

Create an initializer and add this:

Less::Js::Routes::Config.configure do |config|
  config.debug = false                          #default is false
  config.ignore = [/devise/, :users, /admin/]       #default is []
  config.only = [:posts, :comments]             #default is []
end

Debug

Adds debugging info to the javascript file.

Ignore

  • Takes an array of regex or symbols.
  • Will not generate a route if symbol or regex matches.
  • If a symbol is passed the symbol must match the name of the controller without "controller," so for the userscontroller you would just pass :user.
  • If a regex is passed the regex is matched against any part of name (without "_controller"), so /admin/ will match on "admin," "admins," or "administrator." /min/ will match on "min," "admin," etc.
  • If both ignore and only are used, ignore will trump only, taking a "most restrictive approach."

Only

  • Takes an array of regex or symbols.
  • Will only generate a route if symbol or regex matches.
  • If a symbol is passed the symbol must match the name of the controller without "controller," so for the userscontroller you would just pass :user.
  • If a regex is passed the regex is matched against any part of name (without "_controller"), so /admin/ will match on "admin," "admins," or "administrator." /min/ will match on "min," "admin," etc.
  • If both ignore and only are used, ignore will trump only, taking a "most restrictive approach."

Usage

If you have the following in your routes file:

resources :posts do
  resources :comments
end
resources :comments

This will be generated for you:

function post_comments_path(post_id, format){ var _post_id = less_check_parameter(post_id);var _format = less_check_format(format);return '/posts' + '/' + _post_id + '/comments' + _format}
function post_comments_ajax(post_id, format, verb, params, options){ var _post_id = less_check_parameter(post_id);var _format = less_check_format(format);return less_ajax('/posts' + '/' + _post_id + '/comments' + _format, verb, params, options)}
function new_post_comment_path(post_id, format){ var _post_id = less_check_parameter(post_id);var _format = less_check_format(format);return '/posts' + '/' + _post_id + '/comments' + '/new' + _format}
function new_post_comment_ajax(post_id, format, verb, params, options){ var _post_id = less_check_parameter(post_id);var _format = less_check_format(format);return less_ajax('/posts' + '/' + _post_id + '/comments' + '/new' + _format, verb, params, options)}
function edit_post_comment_path(post_id, id, format){ var _post_id = less_check_parameter(post_id);var _id = less_check_parameter(id);var _format = less_check_format(format);return '/posts' + '/' + _post_id + '/comments' + '/' + _id + '/edit' + _format}
function edit_post_comment_ajax(post_id, id, format, verb, params, options){ var _post_id = less_check_parameter(post_id);var _id = less_check_parameter(id);var _format = less_check_format(format);return less_ajax('/posts' + '/' + _post_id + '/comments' + '/' + _id + '/edit' + _format, verb, params, options)}
function post_comment_path(post_id, id, format){ var _post_id = less_check_parameter(post_id);var _id = less_check_parameter(id);var _format = less_check_format(format);return '/posts' + '/' + _post_id + '/comments' + '/' + _id + _format}
function post_comment_ajax(post_id, id, format, verb, params, options){ var _post_id = less_check_parameter(post_id);var _id = less_check_parameter(id);var _format = less_check_format(format);return less_ajax('/posts' + '/' + _post_id + '/comments' + '/' + _id + _format, verb, params, options)}
function posts_path(format){ var _format = less_check_format(format);return '/posts' + _format}
function posts_ajax(format, verb, params, options){ var _format = less_check_format(format);return less_ajax('/posts' + _format, verb, params, options)}
function new_post_path(format){ var _format = less_check_format(format);return '/posts' + '/new' + _format}
function new_post_ajax(format, verb, params, options){ var _format = less_check_format(format);return less_ajax('/posts' + '/new' + _format, verb, params, options)}
function edit_post_path(id, format){ var _id = less_check_parameter(id);var _format = less_check_format(format);return '/posts' + '/' + _id + '/edit' + _format}
function edit_post_ajax(id, format, verb, params, options){ var _id = less_check_parameter(id);var _format = less_check_format(format);return less_ajax('/posts' + '/' + _id + '/edit' + _format, verb, params, options)}
function post_path(id, format){ var _id = less_check_parameter(id);var _format = less_check_format(format);return '/posts' + '/' + _id + _format}
function post_ajax(id, format, verb, params, options){ var _id = less_check_parameter(id);var _format = less_check_format(format);return less_ajax('/posts' + '/' + _id + _format, verb, params, options)}
function comments_path(format){ var _format = less_check_format(format);return '/comments' + _format}
function comments_ajax(format, verb, params, options){ var _format = less_check_format(format);return less_ajax('/comments' + _format, verb, params, options)}
function new_comment_path(format){ var _format = less_check_format(format);return '/comments' + '/new' + _format}
function new_comment_ajax(format, verb, params, options){ var _format = less_check_format(format);return less_ajax('/comments' + '/new' + _format, verb, params, options)}
function edit_comment_path(id, format){ var _id = less_check_parameter(id);var _format = less_check_format(format);return '/comments' + '/' + _id + '/edit' + _format}
function edit_comment_ajax(id, format, verb, params, options){ var _id = less_check_parameter(id);var _format = less_check_format(format);return less_ajax('/comments' + '/' + _id + '/edit' + _format, verb, params, options)}
function comment_path(id, format){ var _id = less_check_parameter(id);var _format = less_check_format(format);return '/comments' + '/' + _id + _format}
function comment_ajax(id, format, verb, params, options){ var _id = less_check_parameter(id);var _format = less_check_format(format);return less_ajax('/comments' + '/' + _id + _format, verb, params, options)}

Note

  • Nested routes are generated.
  • new and edit routes are generated.
  • Singular and plural routes are generated.
  • For each route two functions are generated:

Functions

*_path Functions:

  • Params: id(s), format
    • id(s): The integer ids for the resource you're accessing. Default is ''.
    • format: The format you would like returned for the resource, example: "js", "json," "xml," etc. Default is ''.
  • Returns: string that is the path to the resource.

Example:

//function comments_path(format)
comments_path()
"/comments"
//function comments_path(format)
comments_path('json')
"/comments.json"
//function comment_path(id, format)
comment_path(1)
"/comments/1"
//function comment_path(id, format)
comment_path(1, 'js')
"/comments/1.js"
//function edit_comment_path(id, format)
edit_comment_path(17, 'json')
"/comments/17/edit.json"
//function post_comments_path(post_id, format)
post_comments_path(1)
"/posts/1/comments"
//function post_comment_path(post_id, id, format)
post_comment_path(1, 2)
"/posts/1/comments/2"
//function post_comment_path(post_id, id, format)
post_comment_path(1, 2, 'xml')
"/posts/1/comments/2.xml"

*_ajax Functions:

  • Params: id(s), format, verb, params, options

    • id(s): The integer ids for the resource you're accessing. Default is ''.
    • format: The format you would like returned for the resource, example: "js", "json," "xml," etc. Default is ''.
    • verb: The HTTP verb you'd like to use, 'get,' 'post,' 'put,' or 'delete.' Default is 'get'.
    • params: Additional params you'd like to pass to the ajax request. Example: {name: 'steve'}
    • options: Additional ajax options you'd like to pass to the javascript library ajax function.
      • If no "error" ("onFailure") option is supplied the following will be executed if an error occurs:

    function(r, status, errorthrown){alert(status + ": " + errorthrown)}

If neither "success" ("onSuccess") or "complete" ("onComplete") options are supplied the following will be executed when the request completes with no error:

function(r){eval(r.responseText)};

This means that by default rjs or any javascript that your app returns will be eval'd.

  • Returns: Nothing. Everything happens async so there's nothing to return.

Example:

//function post_comments_ajax(post_id, format, verb, params, options)
post_comments_ajax(1, 'js')
post_comments_ajax(1, 'json', null, null, {success: function(r){console.log(r)}}))
//function posts_ajax(format, verb, params, options)
post_ajax('js', 'post', {post_title: "title", post_body: "body"})
I hope you'll join us for LessConf 2012, Feb 23-24, 2012 in Atlanta Ga.
We're releasing our first ebook titled "How we built our consultancy to over $1,000,000 a year in revenue." Get early access to the ebook.

1 Comment

Werloorcext
Werloorcext said on November 22, 2011

lesseverything.com : #grabbed[n – big tits lesbian vintage dgbxwwjsfks480.blogspot.com/2011/11/big-tits-lesbian-vintage.html hennesy is looking for someone to ream her

Leave a Comment

About Allan
Allan loves his family more than breathing. He lives in Panama City, Florida & grew up washing cars at his family's car washes. Oh and Allan hasn't worn underwear since 2004.

You Should...

Follow Allan on Twitter
Friend Allan on Facebook
Subscribe
LessEverything Copyright 2011 LessEverything.com
We don't like footers, they're kinda boring