1

For my public files, I usually use:

@routes.Assets.at("data/table.txt")

in regular html

However, I have a javascript with the variable link

var file = "table.txt"//This changes depending on the code
var link = "data/"+ file;

And would like to run some jquery function like this

$('#mydiv').CSVToTable(@routes.Assets.at(link))

But this does not work. My questions

  1. Why does play use @routes.Assets.at? is it more secure in some way? it just seems to prepend "assets" to whatever is passed in to it. Is there a disadvantage to just using the text "/assets/data/table.txt"
  2. How would I pass a javascript variable into @routes.Assets.at

Thanks in advanced!

1 Answer 1

2
  1. It's much more... comfortable (who'd guest? :)) just when you are using assets' routes you are able to switch ie. between CDN's much faster. If you'll put hardcoded URL's everywhere you'll need to fix all of them when you'll change your app's location.
  2. There are some possibilities also with common JavaScript, you can for an example write the route's value to JS' someSpecificRoute = '@routes.Assets.at(link)'; and than use it in your script

    $('#mydiv').CSVToTable(someSpecificRoute);
    

    maybe you can also use javascriptRoutes however you'll need to decide yourself. In general using it is good idea when you are planning to use them more than once or twice.

  3. Finally you can just use a hardcoded path and common JavaScript techniques for sending additional params. In such case I can only advice that for the security reasons it will the best if you'll validate if passed arguments fits allowed type and/or range.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. I tried: var link = "@routes.Assets.at(data/"+dirName+"/"+fileName+")"; and $('#mydiv').CSVToTable(link); But it failed... Its not processing the scala code, rather its just putting everything as is with the @ symbol.. Am I doing something wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.