Expand my Community achievements bar.

SOLVED

Variables in Fragments

Avatar

Former Community Member

Hi There,

Is it possible to make use of the form variables in Fragments?

i.e. - Lets say I have a fragment and create a variable within the fragment xdp (in Designer:  File > Form Properties > Variables) is it possible to access the fragment variable in script??

function ufSetVar()
{
   testVar.value ="test1";
   xfa.host.messageBox(testVar.value);
}

I am trying in a script within the fragment, and it only gives me an error: e.g, testVar is not defined

I guess I could use a hidden text field in the fragment to store my variable, but it would be nicer to use the Variable method if possible...

Thanks

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

The scope of a variable defined in script is for the duration of the script. The concept of a global var is contained within the Form Properties/variables but that is not carried through for a fragment. You might be able to create a variable in a scripting fragment (I know the functions end up being global) and have that as a global but I have not tried that. I think the hidden field might be your best bet.

I also found this blog by John Brinkman (one of the creators of XFA) that you might find useful. This particulr entry discusses custom properties on objects ...you coudl use  that to pass your information between fragment and main code. I tried it and I was able to access the customer property from the main when set in the frag.

http://blogs.adobe.com/formfeed/2009/02/scope_of_javascript_objects.html

Paul

View solution in original post

2 Replies

Avatar

Correct answer by
Former Community Member

The scope of a variable defined in script is for the duration of the script. The concept of a global var is contained within the Form Properties/variables but that is not carried through for a fragment. You might be able to create a variable in a scripting fragment (I know the functions end up being global) and have that as a global but I have not tried that. I think the hidden field might be your best bet.

I also found this blog by John Brinkman (one of the creators of XFA) that you might find useful. This particulr entry discusses custom properties on objects ...you coudl use  that to pass your information between fragment and main code. I tried it and I was able to access the customer property from the main when set in the frag.

http://blogs.adobe.com/formfeed/2009/02/scope_of_javascript_objects.html

Paul

Avatar

Former Community Member

Thanks Paul,

Yes, this post (linked from that page) contains instructions to do exactly that:

http://blogs.adobe.com/formfeed/2008/10/form_variables.html

This is what I changed my function to, and it worked!

function ufSetVar()
{

    if (TextField1.desc.nodes.namedItem("vFortyTwo") == null)
    {
        var vNewVariable = xfa.form.createNode("text", "vFortyTwo");
        vNewVariable.value = "abc";
        TextField1.desc.nodes.append(vNewVariable);
    }
   
   xfa.host.messageBox("val:" + TextField1.desc.nodes.namedItem("vFortyTwo").value);
}

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----