access controller object from a javascript file in sails js

788 Views Asked by At

I am new to sails js. I am passing a object from controller to view in sails js. I can access to that object from ejs (embedded javascript) file. But I need to access that object from a javascript file. Should I need to use the object in a hidden field or is there a better way to get the object to javascript file.

2

There are 2 best solutions below

0
On

If you need to access it Javascript then you can simply save the value to a JavaScript object in your view page and this access it after page load.

viewFile.ejs

<script>
    window.data = <%= JSON.stringify(data.dataFromController) %>    
</script>
<script src="/jqueryExample.js"></script>

jqueryExample.js

$(function(){
   data = window.data;
})
0
On

You will have to send the data in some hidden field. It is not possible to access controller variables from front-end.

Also, you could use sockets.