<?xml version="1.0" encoding="UTF-8"?>
<page>
  <body>&lt;p&gt;
  JavascriptRoutes converts your Rails routes into JavaScript. You can then access (generate) them in the browser (or any other JS environment). It supports both normal and named routes, and creates helper functions for the latter.
&lt;/p&gt;

&lt;h3&gt;Installing&lt;/h3&gt;

Download from &quot;Github&quot;:https://github.com/toretore/javascript_routes/tree and put in @vendor/plugins@.

&lt;h3&gt;Using&lt;/h3&gt;

&lt;p&gt;
  The plugin will generate _public/javascripts/routes.js_ when the application starts. The file will vary in size depending on how many routes you have in routes.rb. The &quot;required&quot; parts are around 3kb. To use it, simply include it like any other JavaScript file in your layout:
&lt;/p&gt;

[html]
&lt;script type=&quot;text/javascript&quot; src=&quot;/javascripts/routes.js&quot;&gt;&lt;/script&gt;
[/html]

&lt;p&gt;
  The file will define two objects that will be available to the browser. A @Route@ encapsulates a single route, while @Routes@ contain all routes (it's an array). @Routes.named@ contains all the named routes. To generate a route, call @Routes.generate@:
&lt;/p&gt;

[javascript]
Routes.generate({controller:'foo', action:'foo'})
Routes.generate({controller:'bar', action:'baz', baz:'quux'}, {host:'example.com'})
[/javascript]

&lt;p&gt;
  If no routes are found to match the parameters, it will return false. The generate function takes an object (hash) with name/value mappings and an optional @options@ object. The available options are:
&lt;/p&gt;

&lt;dl&gt;
  &lt;dt&gt;onlyPath&lt;/dt&gt;
    &lt;dd&gt;Don't include the domain&lt;/dd&gt;
  &lt;dt&gt;includeSlash&lt;/dt&gt;
    &lt;dd&gt;Include the last slash in the path&lt;/dd&gt;
  &lt;dt&gt;escape&lt;/dt&gt;
    &lt;dd&gt;Set to false to prevent the escaping of special characters&lt;/dd&gt;
  &lt;dt&gt;protocol&lt;/dt&gt;
    &lt;dd&gt;Protocol other than 'http://' to use&lt;/dd&gt;
  &lt;dt&gt;host&lt;/dt&gt;
    &lt;dd&gt;Hostname&lt;/dd&gt;
  &lt;dt&gt;port&lt;/dt&gt;
    &lt;dd&gt;Port number&lt;/dd&gt;
  &lt;dt&gt;noDefaults&lt;/dt&gt;
    &lt;dd&gt;Don't use &lt;code&gt;Routes.defaultParams&lt;/code&gt; when generating URL&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;
  If you want to use a named route, it will have defined a convenience method on the @Routes@ object:
&lt;/p&gt;


[javascript]
Routes.home()
Routes.articles()
Routes.article(1)
Routes.article({id:1, foo:'bar'})
[/javascript]

&lt;p&gt;
  These functions can, like their Rails equivalents, take either a hash with name/value mappings, or all required arguments in the correct order. They also take an optional second (or last) argument @options@.
&lt;/p&gt;

&lt;h3&gt;Default parameters and options&lt;/h3&gt;

&lt;p&gt;
  The @Routes@ object has two properties, @defaultParams@ and @defaultOptions@, that can be filled with default values for the @params@ and @options@ arguments passed when generating URLs. You could, for example, set a default controller name:
&lt;/p&gt;

[javascript]
Routes.defaultParams.controller = 'articles';
Routes.generate({action:'show', id:1});
// =&gt; &quot;/articles/1&quot; (if RESTful)
//You can set any parameter you want
Routes.defaultParams.foo = 'bar';
Routes.generate({controller:'users', :action:'index'});
// =&gt; &quot;/users?foo=bar&quot;
Routes.generate({controller:'users', :action:'index', foo:'rab'});
// =&gt; &quot;/users?foo=rab&quot;
delete Routes.defaultParams.foo;
Routes.defaultParams.id = 1;
Routes.user();
//=&gt; &quot;/users/1&quot;
Routes.user(2);
//=&gt; &quot;/users/2&quot;
[/javascript]

&lt;p&gt;
  @Routes.defaultParams.action@ is already set to &quot;index&quot;, which is the default action for any controller. You can @delete@ this if that's not what you want of course. Parameters passed to the generate method or default parameters from a named route will overwrite parameters from @defaultParams@.
&lt;/p&gt;

&lt;p&gt;
  @Routes.defaultOptions@ contains the default values passed as the @options@ (second) argument when generating URLs. Some values, like @host@ and @port@ will already have been set to the current host name and port number.
&lt;/p&gt;

[javascript]
Routes.defaultOptions.host = 'example.com';
Routes.generate({controller:'users'});
//=&gt; &quot;http://example.com/users&quot;
Routes.defaultOptions.onlyPath = true;
Routes.generate({controller:'users'});
//=&gt; &quot;/users&quot;
Routes.defaultParams.foo = 'bar';
Routes.generate({controller:'users'});
//=&gt; &quot;/users?foo=bar&quot;
Routes.defaultParams.noDefaults = true;
Routes.generate({controller:'users'});
//=&gt; &quot;/users&quot;
[/javascript]

&lt;p&gt;
  As with @defaultParams@, values in @defaultOptions@ will be overwritten by options passed in at generation time.
&lt;/p&gt;</body>
  <created-at type="datetime">2007-06-29T16:38:40-04:00</created-at>
  <id type="integer">18</id>
  <parent-id type="integer" nil="true"></parent-id>
  <published type="boolean">true</published>
  <slug>javascript_routes</slug>
  <summary></summary>
  <title>JavaScript Routes</title>
  <updated-at type="datetime">2008-04-17T10:08:37-04:00</updated-at>
</page>
