Skip to content

This gem will rake your routes, and generate a javascript file that will let you easily access named routes from your client side JS.

License

Notifications You must be signed in to change notification settings

Velkitor/coffee-grounds

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CoffeeGrounds

Purpose

I wrote this to prove an easy way to access your rails routes from your client side javascript. Have you ever wanted to do something like $.get( blog_post_path({id: 1}) )? Well this will let you do that!

How do I use it?

(1) add this gem to your Gemfile

* gem 'coffee-grounds'

(2) bundle install

(3) generate the javascript file

* rake coffee-grounds:compile-asset
  This will generate a coffee-grounds.js file in your app/assets/javascripts folder.

All of your named routes will show up under an App object that is defined on the window. To access a route all you need to do is call App.routes.<route_name_path>() This function takes optional arguments, the first optional argument is an object containing identifiers and their values, and the second argument is the requested format.

Cool additions

Example: You have a rails route that is: /foo/:id(.format) (named as foo) and you would like to get the path for an id of ‘bar’ and request it as html. App.routes.foo_path( { id: “bar” }, “html” ) That will return “/foo/bar.html”

I have also added easy ways to get and post data.

  • App.request.<named_route>_path() will execute a JQuery get to the specified route.

  • App.post.<named_route>_path() will execute a JQuery post to the specified route.

Both functions take the following optional arguments:

  • identifiers: an object with keys and values corresponding to your defined rails routes

  • params: an object containing the params to be sent with the request. (This is just passed down to JQuery )

  • function: this will be executed on a successful callback ( also passed to JQuery )

  • format: This format is both used in building the url, and passed to JQuery so that JQuery knows whats going on.

I also added a helper function to help init your coffee script on your pages, just include the following after your application javascript.

* haml =coffee_grounds_script_tag.html_safe
* erb <%= coffee_grounds_script_tag.html_safe %>

This will attempt to call:

* App.<controller_name>.init()
* App.<controller_name>.<action_name>.init()

You can specify the JavaScript name space by passing it in to the helper function.

Your CoffeeScript file should have a section defining your init functions for each action. It would look something like:

App.home =
 init: ->
  Bind your clicks and do some cool stuff for all actions in this controller
 index:
   init: ->
     Bind your clicks and do cool stuff for only the index.

About

This gem will rake your routes, and generate a javascript file that will let you easily access named routes from your client side JS.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published