access jelly variable inside <script> tag
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2015 08:36 PM
I have a UI Page that retrieves data like this:
<g:evaluate var='jvar_events' object='true'>
var dt = new Date(2015, 0, 15, 0,0,0,0);
var gr = new isUtils();
var events = gr.getArrayOfObjects('74ad1ff3c611227d01d25feac2af603f', dt);
events;
</g:evaluate>
That executes fine, and jvar_events is an array of objects (I confirmed this with a <j:forEach... loop.
I need to pass jvar_events to a jQuery function inside the <script></script> block.
<script>
var $j_custom = jQuery.noConflict(true);
$j_custom(document).ready(function () {
parm1:test,
parm2: test,
parm3: ${jvar_events}
</script>
I've tried ${jvar_events}, '$jvar_events}, neither works.
I've also tried executing this inside the <script> and using events as parm3:
var dt = new Date(2015, 0, 15, 0,0,0,0);
var gr = new isUtils();
var events = gr.getArrayOfObjects('74ad1ff3c611227d01d25feac2af603f', dt);
when I create the same array inside the <script> tags with javascript it works fine, so I must not be accessing the jelly variable properly.
I cannot find any valid Wiki or articles on the subject.
Any assistance would be greatly appreciated.
Kevin
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2015 07:17 AM
Hi Kevin,
You should be able to pass the value, but it will be passed as a string. If it's just an array you could just do a join and then a split when referencing it in the client side script.
<g:evaluate var='jvar_events'>
var dt = new Date(2015, 0, 15, 0,0,0,0);
var gr = new isUtils();
var events = gr.getArrayOfObjects('74ad1ff3c611227d01d25feac2af603f', dt);
events.join();
</g:evaluate>
<script>
var eventsStr = '${jvar_events}';
var events = eventsStr.split(',');
var $j_custom = jQuery.noConflict(true);
$j_custom(document).ready(function () {
parm1:test,
parm2: test,
parm3: events
}
</script>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2015 07:38 AM
I added the following to the script:
var ev = '${jvar_events}';
alert('${jvar_events}');
alert("${jvar_events}");
alert(ev);
They all display org.mozilla.javascript.NativeArray@........
I was hoping it would return [Object]
I guess I just need to figure out how to pull the data from the array;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2015 07:45 AM
You could also JSON encode this so you can pass an object.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2015 07:49 AM
How would I JSON encode it ??
That may be my best bet.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2015 07:55 AM
Guess I should have posted a link.
I think you should be able to use the following on the server:
var encodedObject = new JSON().encode(object);
Then use the following to decode on the client:
