cYn
cYn

Reputation: 3381

Reference Scala template code in a separate Javascript File in Play Framework

If I have something like this in Application.scala.html

<div id="action">Some content</div>

<script>
    $('action').click(function(){
         window.open(@routes.Application.index());
    )};
</script>

This works fine, no problem.

But if I put the script in its own Javascript file, I can no longer use the @routes.Application.index() reference. Is is possible in Play to reference Scala templates in a separate Javascript file place under public/javascripts?

Upvotes: 0

Views: 422

Answers (1)

danielnixon
danielnixon

Reputation: 4268

Have you considered something like this?

<div id="action" data-target="@routes.Application.index()">Some content</div>

<script>
    $('action').click(function(){
         var target = $(this).data('target');
         window.open(target);
    )};
</script>

You can then extract the JavaScript to its own file.

Although in this particular case an <a> tag would be more appropriate (for a number of reasons, including accessibility).

Upvotes: 1

Related Questions