=head1 NAME Mojolicious::Plugin::JSUrlFor - Mojolicious "url_for" helper for javascript =head1 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
<%= js_url_for%> # 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 # 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'}); =head1 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