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.
Asked
Active
Viewed 776 times
1
2 Answers
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