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!

Omar Wagih
  • 8,504
  • 7
  • 59
  • 75

1 Answers1

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.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182
  • 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 – Omar Wagih Sep 22 '12 at 05:33