5

Using sails.js as a framework for a node.js app I'm building.

I'm trying to retrieve information previously set by a user (i.e. settings for my app) from the back end, and then use these settings in my client-side javascript.

I currently have a controller which retrieves the information from the database and passes it to the view where it will be needed.

return res.view('dashboard', { configs: configurations });

I know it is accessible when rendering html, but don't know how to pass it to the javascript environment running on the browser.

Any tips would be amazing!

To clarify, I'm not trying to render this information in the html page (i.e. the method of using <%= variable %>, I want to use it in the javascript running on the page.

nbdy_
  • 729
  • 1
  • 9
  • 18
  • Try method from this question http://stackoverflow.com/questions/16098397/pass-variables-to-javascript-in-expressjs – vanadium23 Apr 24 '15 at 08:34

2 Answers2

3

If i understood your question correctly; you can use the server side variable in your script tag.

<script>

    var something = <%= serversideVariable %>;

    var  <%= serversideVariable %> = "some value";

</script>

This would work as javascript.

serkan
  • 6,885
  • 4
  • 41
  • 49
  • Looking promising... I'm getting a different error but it is recognising the data now. Will check back in when it works. Thanks – nbdy_ Apr 24 '15 at 08:49
0

You can generate something like this in your view. During page loading this script will be executed and any following script will have access to GlobalSettings.

<script type="text/javascript">
var GlobalSettings = 
{
    //.. JSON with parameters for client
}
</script>
light_keeper
  • 627
  • 5
  • 15