Objects returned from g/g2:eval in Jelly Scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2016 12:45 AM
OK, So I have the following simple UI Page Jelly XML , but it's returning me some strange results.
This is just a test page to prove something is working.... I have an object in JS that I want to pass out to Jelly. I want to then check for an error property, and if I don't have one iterate over each prop and display the Key and Value of the prop.
The first thing I did was write a simple test Jelly XML to prove that I could have an object in JS, pass it off to Jelly via g2:evaluate (the object will eventually be passed into this page dynamically) and use Jellys j2:if (actually I think it will end up being a choose... but it doesn't matter, I was proving the JEXL works correctly) to check for an error prop and display it, or iterate through the props.
Unfortunately I didn't get to the iterating though the props as I'm having trouble getting the object into a jvar, but I can use a javascript var if I just do a j2:evaluate without a var param.
So what am I missing here... why cannot I pass the var back out from g2:eval using the var prop to populate a jvar_ variable?
Also why are the j2:ifs for $[my_magical_js_var] and $[my_magical_js_var.error] returning false when I know that the object and prop exists. I don't mind using the .hasOwnProperty (and in fact I prefer it) but I still want to understand why the other j2:if statements are not returning as expected!
As you can see in both evaluates I output the object to the log and it all works as I would have expected.
Very confusing!
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<!-- Initial Test to see if an object I manually set works... It doesn't! -->
<g2:evaluate var="jvar_my_test_object" object="true">
var thingy = {"error":"this is some random error"}
/*
This outputs the following in the log....
---------------------------------------------
Log Object: thingy
Object
error: string = this is some random error
---------------------------------------------
*/
JSUtil.logObject(thingy, 'thingy');
thingy;
</g2:evaluate>
<!-- This outputs "Error Prop: ::" -->
<div>Error Prop: :$[jvar_my_test_object.error]:</div>
<!-- This echos "Object: :[object Object]:" -->
<div>Object: :$[jvar_my_test_object]:</div>
<!-- This if test doesn't work.... -->
<j2:if test="$[jvar_my_test_object]">
First If test worked
</j2:if>
<!-- This if test doesn't work.... -->
<j2:if test="$[jvar_my_test_object.error]">
First If ERROR test worked
</j2:if>
<!-- This if test doesn't work.... -->
<j2:if test="$[jvar_my_test_object.hasOwnProperty('error')]">
First If hasOwnProperty test worked
</j2:if>
<!-- OK. No try using a JS var -->
<g2:evaluate>
var my_magical_js_var = {"error":"this is some random error"}
/*
This outputs the following in the log....
---------------------------------------------
Log Object: my_magical_js_var
Object
error: string = this is some random error
---------------------------------------------
*/
JSUtil.logObject(my_magical_js_var , 'my_magical_js_var ');
</g2:evaluate>
<!-- This outputs "Error Prop: :this is some random error:" -->
<div>Error Prop: :$[my_magical_js_var .error]:</div>
<!-- This echos "Object: :[object Object]:" -->
<div>Object: :$[my_magical_js_var ]:</div>
<!-- This if test doesn't work.... -->
<j2:if test="$[my_magical_js_var]">
Second If test worked
</j2:if>
<!-- This if test doesn't work.... -->
<j2:if test="$[my_magical_js_var.error]">
Second If ERROR test worked
</j2:if>
<!-- This if test does.... -->
<j2:if test="$[my_magical_js_var.hasOwnProperty('error')]">
Second If hasOwnProperty test worked
</j2:if>
</j:jelly>
This returns the following text.
Error Prop: ::
Object: :[object Object]:
Error Prop: :this is some random error:
Object: :[object Object]:
Second If hasOwnProperty test worked
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2016 04:11 AM
Hi James,
The Jelly if condition statement seems to be working differently. If test expects exactly true/false. I have changed the if conditions like below, and they started working.
<!-- This if test doesn't work.... -->
<j2:if test="$[typeof my_magical_js_var == 'object']">
srini1 </j2:if>
<!-- This if test doesn't work.... -->
<j2:if test="$[my_magical_js_var.error== 'this is some random error']">
srini2 Second If ERROR test worked
</j2:if>
Thanks
Srinivas
Hope that helps
Mark this answer as helpful/correct if it does so
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2016 04:40 AM
Hi Srinivas,
Yes that explains why the if statements are not working. It's a true bool comparison which makes sense.
It still doesn't explain why I cannot get the js object out into a jvar_ variable.
At least it gives me a starting point
Sent from my iPhone