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:
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).
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.
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
Adds debugging info to the javascript file.
If you have the following in your routes file:
resources :posts do resources :comments end resources :comments
This will be generated for you:
function postcommentspath(postid, format){ var _postid = lesscheckparameter(postid);var _format = lesscheckformat(format);return ‘/posts’ + ‘/’ + _postid + ‘/comments’ + format} function postcommentsajax(postid, format, verb, params, options){ var postid = lesscheckparameter(postid);var _format = lesscheckformat(format);return lessajax(‘/posts’ + ‘/’ + postid + ‘/comments’ + format, verb, params, options)} function newpostcommentpath(postid, format){ var _postid = lesscheckparameter(postid);var _format = lesscheckformat(format);return ‘/posts’ + ‘/’ + _postid + ‘/comments’ + ‘/new’ + format} function newpostcommentajax(postid, format, verb, params, options){ var _postid = lesscheckparameter(postid);var _format = lesscheckformat(format);return lessajax(‘/posts’ + ‘/’ + postid + ‘/comments’ + ‘/new’ + format, verb, params, options)} function editpostcommentpath(postid, id, format){ var _postid = lesscheckparameter(postid);var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return ‘/posts’ + ‘/’ + _postid + ‘/comments’ + ‘/’ + id + ‘/edit’ + _format} function editpostcommentajax(postid, id, format, verb, params, options){ var _postid = lesscheckparameter(postid);var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return lessajax(‘/posts’ + ‘/’ + postid + ‘/comments’ + ‘/’ + id + ‘/edit’ + _format, verb, params, options)} function postcommentpath(postid, id, format){ var postid = lesscheckparameter(postid);var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return ‘/posts’ + ‘/’ + _postid + ‘/comments’ + ‘/’ + id + _format} function postcommentajax(postid, id, format, verb, params, options){ var postid = lesscheckparameter(postid);var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return lessajax(‘/posts’ + ‘/’ + postid + ‘/comments’ + ‘/’ + id + _format, verb, params, options)} function postspath(format){ var format = lesscheckformat(format);return ‘/posts’ + _format} function postsajax(format, verb, params, options){ var format = lesscheckformat(format);return lessajax(‘/posts’ + format, verb, params, options)} function newpostpath(format){ var _format = lesscheckformat(format);return ‘/posts’ + ‘/new’ + _format} function newpostajax(format, verb, params, options){ var _format = lesscheckformat(format);return lessajax(‘/posts’ + ‘/new’ + format, verb, params, options)} function editpostpath(id, format){ var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return ‘/posts’ + ‘/’ + _id + ‘/edit’ + _format} function editpostajax(id, format, verb, params, options){ var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return lessajax(‘/posts’ + ‘/’ + id + ‘/edit’ + _format, verb, params, options)} function postpath(id, format){ var id = lesscheckparameter(id);var _format = lesscheckformat(format);return ‘/posts’ + ‘/’ + _id + _format} function postajax(id, format, verb, params, options){ var id = lesscheckparameter(id);var _format = lesscheckformat(format);return lessajax(‘/posts’ + ‘/’ + id + _format, verb, params, options)} function commentspath(format){ var format = lesscheckformat(format);return ‘/comments’ + _format} function commentsajax(format, verb, params, options){ var format = lesscheckformat(format);return lessajax(‘/comments’ + format, verb, params, options)} function newcommentpath(format){ var _format = lesscheckformat(format);return ‘/comments’ + ‘/new’ + _format} function newcommentajax(format, verb, params, options){ var _format = lesscheckformat(format);return lessajax(‘/comments’ + ‘/new’ + format, verb, params, options)} function editcommentpath(id, format){ var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return ‘/comments’ + ‘/’ + _id + ‘/edit’ + _format} function editcommentajax(id, format, verb, params, options){ var _id = lesscheckparameter(id);var _format = lesscheckformat(format);return lessajax(‘/comments’ + ‘/’ + id + ‘/edit’ + _format, verb, params, options)} function commentpath(id, format){ var id = lesscheckparameter(id);var _format = lesscheckformat(format);return ‘/comments’ + ‘/’ + _id + _format} function commentajax(id, format, verb, params, options){ var id = lesscheckparameter(id);var _format = lesscheckformat(format);return lessajax(‘/comments’ + ‘/’ + _id + _format, verb, params, options)}
Params: id(s), format
Returns: string that is the path to the resource.
Example:
//function commentspath(format) commentspath() ”/comments" //function commentspath(format) commentspath(‘json’) “/comments.json” //function commentpath(id, format) commentpath(1) “/comments/1” //function commentpath(id, format) commentpath(1, ‘js’) “/comments/1.js” //function editcommentpath(id, format) editcommentpath(17, ‘json’) “/comments/17/edit.json” //function postcommentspath(postid, format) postcommentspath(1) “/posts/1/comments” //function postcommentpath(postid, id, format) postcommentpath(1, 2) “/posts/1/comments/2” //function postcommentpath(postid, id, format) postcomment_path(1, 2, ‘xml’) “/posts/1/comments/2.xml”
Params: id(s), format, verb, params, options
options: Additional ajax options you’d like to pass to the javascript library ajax function.
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®{eval(r.responseText)};
This means that by default rjs or any javascript that your app returns will be eval’d.
Example:
//function postcommentsajax(postid, format, verb, params, options) postcommentsajax(1, ‘js’) postcommentsajax(1, ‘json’, null, null, {success: function®{console.log®}})) //function postsajax(format, verb, params, options) postajax(‘js’, ‘post’, {posttitle: “title”, post_body: “body”})
If you wanted it to build a product you’d find a way to get time to work on it. If you really wanted to start that new hobby you’d sacrifice something to find the time and money to do it.
I'll define a "Wannabe Entrepreneur" as someone who has never made money from their businesses. Here are the different types of wannabes.
In the past few years I've built go-carts, built a 200+ sq ft workshop, written several eBooks. How do I create a life where I have time to work on side projects?
Receive 5 Software projects mistakes we have made over the years and how to avoid them.