1

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.

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
Gayan Charith
  • 7,301
  • 2
  • 20
  • 32

2 Answers2

1

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.

myusuf
  • 11,810
  • 11
  • 35
  • 50
1

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;
})
Meeker
  • 5,979
  • 2
  • 20
  • 38