The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::JSUrlFor - Mojolicious "url_for" helper for javascript

SYNOPSIS

  # Mojolicious
  $self->plugin('JSUrlFor');

  # Mojolicious::Lite
  plugin 'JSUrlFor';

  # In you application
  my $r = $self->routes;
  $r->get('/messages/:message_id')->to('messages#show')->name('messages_show');

  # In your layout template
  <head>
  <%= js_url_for%>
  </head>

  # In your javascript
  $.getJSON( url_for( 'messages_show', {message_id: 123} ), params, function() { ... } )


  # Instead of helper you can use generator for generating static file
  ./your_app.pl generate js_url_for public/static/url_for.js

  # And then in your layout template
  <head>
    <script type="text/javascript" src='/static/url_for.js'> </script>
  </head>

  # Or let it generate on the fly
  # Can be usefull if you have only RESTful API without templates and you want to provide routes names for UI
  $self->plugin('JSUrlFor', {route => '/javascript/url.js'});
  <head>
    <script type="text/javascript" src='/javascripts/url.js'> </script>
  </head>

DESCRIPTION

I like Mojlicious routes. And one feature that I like most is that you can name your routes. So, you can change your routes without rewriting a single line of dependent code. Of course this works if you use routes names in all of your code. You can use routes name everywhere except... javascript. But with <LMojolicious::Plugin::JSUrlFor> you can use routes names really everywhere. This plugin support mounted (see <LMojolicious::Plugin::Mount> ) apps too.

Mojolicious::Plugin::JSUrlFor contains only one helper that add ulr_for function to your client side javascript.

HELPERS

js_url_for

In templates <%= js_url_for %>

This helper will add url_for function to your client side javascript.

In "production" mode this helper will cache generated code for javascript "url_for" function

CONFIG OPTIONS

route

Simulate static javascript file. It can be usefull if you have RESTfull API and want to provide js file with routes.

GENERATORS

js_url_for

./your_app.pl generate js_url_for $relative_file_name

This command will create $relative_file_name file with the same content as "js_url_for" helper creates. Then you should include this file into your layout template with "script" tag.

METHODS

Mojolicious::Plugin::JSUrlFor inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

  $plugin->register;

Register plugin in Mojolicious application.

AUTHOR

Viktor Turskyi <koorchik@cpan.org>

BUGS

Please report any bugs or feature requests to Github https://github.com/koorchik/Mojolicious-Plugin-JSUrlFor/

Also you can report bugs to CPAN RT

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.