7 Replies Latest reply on Mar 21, 2007 5:14 AM by ilya_shaikovsky

    store xml string in a hidden field?

    snod0g

      how can i use ajax4jsf to have a4j:commandbutton that runs a method on my bean that updates a property on the bean (xmlString), and then have that property mapped to a hidden field on the page that is rerendered as having that xmlString value, and then a javascript function is run which will use that hidden field value to populate some stuff?

        • 1. Re: store xml string in a hidden field?

          It looks for me that you include "the part of the solution" to your question. This solution does not seem to me optimal.
          Is it important for you to use exactly a "hidden field" and have those three steps to update something in the browser?

          • 2. Re: store xml string in a hidden field?
            alexsmirnov

            You can use 'data' attribute for return any Java object to 'oncomplete' function :

            ..............
            <a4j:commandButton action="#{bean.action}" data="#{bean.xmlString}" oncomplete="myfunction(data)" />
            <script>
             function myfunction(xmlString){...}
            </script>
            .........

            There 'bean' is You server-side code part.
            action() method should fill xmlString bean property, accessed by method
            public String getXmlString() {...};

            and 'myfunction' is a client-side JavaScript function.
            By default data object will be serialised by JSON , as far as you can use most of Java types : primitives, String,arrays, Collections, Map or any JavaBean .


            • 3. Re: store xml string in a hidden field?
              snod0g

              FYI the reason why I need the data passed to a javascript function is so that I can use Jack Slocum's YUI-EXT Datagrid: http://www.yui-ext.com/deploy/yui-ext/docs/

              im curious if anyone here has tried integrating ajax4jsf with yui-ext widgets?

              • 4. Re: store xml string in a hidden field?
                snod0g

                alex,

                thanks for the reply! i really appreciate it.

                in your example you pass the data variable to the javascript function myfunction(). but in the documentation it says to use this.data

                Serialized (on default with JSON) data passed on the client by a developer on AJAX request. It's accessible via this.data


                does both work?

                also, you said:
                By default data object will be serialised by JSON , as far as you can use most of Java types : primitives, String,arrays, Collections, Map or any JavaBean .


                can you explain this a little more? i understand that you are saying it will be serialized as JSON, but how are you saying that there is a way to have it returned as other formats as well? (such as string, arrays, etc?)

                thank you!

                • 5. Re: store xml string in a hidden field?

                  with a4j:jsFunction you can call Ajax from javascript and then call other javascript with returned data. So, no button or hidden field are required

                  • 6. Re: store xml string in a hidden field?
                    alexsmirnov

                    JSON serialisation/deserialisation is done transparently for developer :
                    Java primitives or String will be converted to JavaScript primitives.
                    Java arrays or Collections will be converted to JavaScript array :
                    Java :
                    int[] data = { 1,2,3};
                    JavaScript :
                    data[1] -> 1
                    data[2] -> 2
                    ...
                    List data = new ArrayList -> JavaScript array.

                    Maps and Java beans will be converted to JavaScript hash :

                    data.get("foo" ) -> data['foo'] or data.foo

                    data.getFoo() -> data['foo'] or data.foo

                    reference to this.data is a documentation bug.

                    • 7. Re: store xml string in a hidden field?
                      ilya_shaikovsky

                      Documentation issue fixed. Sorry for inconvenience, it will be republished soon.