optional
optional

Reputation: 283

Play framework: javascripts in scala code?

If I have a html document with this:

 <p onclick="hiho('@name')"> clicky </p>


<script>
    function hiho(namevar){
        var1 = @Names.find.where().eq("name", namevar).findUnique();
        if( var1 != null){
            alert("HIHO");
        }
    }
</script>

How do I use the JavaScript variable?

Play won't compile properly because inside

var1 = @Names.find.where().eq("name", namevar).findUnique();

it cannot find the value of namevar.

Upvotes: 0

Views: 554

Answers (2)

BFil
BFil

Reputation: 13096

One, pretty straightforward, solution could be:

<p onclick="hiho('@name')"> clicky </p>

<script>
    var map = {
        '@name': '@Names.find.where().eq("name", name).findUnique()'
    };

    function hiho(namevar){
        var value = map[namevar];
        if( value != null){
            alert("HIHO: " + value);
        }
    }
</script>

Supposing that the name variables are strings.

Upvotes: 2

Tim
Tim

Reputation: 5361

You'll probably want to make an AJAX request back to the server. This page talks about that some toward the bottom, and about the routing code you'll need.

Upvotes: 2

Related Questions