Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

get file upload url for passing to js


Reply

3 Posts   1623 Views

Avatar
slavelabourer

25 October 2010 at 12:15pm Community Member, 25 Posts

So following the execellent advice from my last thread i created some variables and passed them to some js.
I now need to pass the url of a file upload to the js.

$fields->addFieldToTab("Root.Content.Embed", new FileIFrameField('GPSXML', 'Upload and Attach Document'));

public function init() {
      parent::init();
      
      Requirements::themedCSS("layout");
      Requirements::themedCSS("typography");
      Requirements::themedCSS("form");
      
      Requirements::javascriptTemplate('mysite/javascript/maps.js', array(
         'GPSXML' => $this->GPSXML.URL
      ));
      
         
   }

This doesnt seem to pass anything over to the js the alert box for $GPSXML just shows blank. I'm guessing i need to parse out the url as a string? any ideas?

thx guys

Avatar
timwjohn

5 November 2010 at 3:46am (Last edited: 5 November 2010 3:46am), Community Member, 98 Posts

Hi,

I had the same problem as you. By inspecting the Requirements::javascriptTemplate() method I found that you do need to encapsulate the replaceable variable name with quotes. Due to the way the variable is replaced, I would also recommend referencing it only once, and assigning it to a 'real' javascript variable, like so:

PHP:

      Requirements::javascriptTemplate('script.js', array(
         "vimeoID" => $this->vimeoID()
      ));

script.js:

var vimeoID = '$vimeoID';

The Requirements::javascriptTemplate() method is a little used and little documented feature, but a handy one nonetheless. I hope this helps those trying to use it in the future...

Avatar
slavelabourer

7 November 2010 at 4:43pm (Last edited: 7 November 2010 4:52pm), Community Member, 25 Posts

this worked for getting the value of vimeoID because it was just a text field. Here i want to pass the url of the filefieldupload.

it actually works fine if i create a text field and have the user manually enter the url but it would be much better if i could get the url straight from the file upload.

thanks heaps for all your help so far.