How to get variable in script edge #1308
-
I have a controller which passing some data to views
I could read data from the tag HTML in edge, but I couldn't read the data in script tag which I need to use jquery
this code, throw an error, threads is not defined |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Coz script tags are executed in browser and edge templates are rendered on the server. |
Beta Was this translation helpful? Give feedback.
-
You kind of can hack it to work tho <script>
window.edgeVariables = {
threads: {{ threads }}
}
</script> And then use with I haven't tested the code in example above, but something really similar should work |
Beta Was this translation helpful? Give feedback.
-
If you're using inline script on the edge view, you can do:
|
Beta Was this translation helpful? Give feedback.
-
You will have to stringify the object or array when sending it over the wire. Even though the code appears to be in the same file. The data is going over HTTP and you cannot send native data types via HTTP. I personally won't recommend using the following code, since you have blurry understanding of the boundaries and you will run into more issues. <script type="text/javascript">
var threads = {{{ JSON.stringify(threads) }}}
console.log(threads)
</script> |
Beta Was this translation helpful? Give feedback.
-
var threads= {{{ toJSON(threads)}}} |
Beta Was this translation helpful? Give feedback.
You will have to stringify the object or array when sending it over the wire. Even though the code appears to be in the same file. The data is going over HTTP and you cannot send native data types via HTTP.
I personally won't recommend using the following code, since you have blurry understanding of the boundaries and you will run into more issues.