6 Replies Latest reply on Apr 5, 2009 7:50 AM by mugglmenzel

    Mapping javascript array to a bean collection property

    mugglmenzel

      I found this topic:
      http://www.jboss.org/index.html?module=bb&op=viewtopic&t=115530
      in the old A4J forum that was actually never really answered.

      Maybe after some new developments added to richfaces this point is solvable now. Does someone has an idea how to do that?

      Mapping a javascript array to a bean's property which is a collection isn't that unusual, right? Other way round is working properly (bean -> javascript).

      thx + regards
      Michael

        • 1. Re: Mapping javascript array to a bean collection property
          nbelaevski

          Hello Michael,

          You can set converter that a4j:actionparam component will use. Also take a look at org.richfaces.json.* package.

          • 2. Re: Mapping javascript array to a bean collection property
            mugglmenzel

            I wrote a nice converter using the JSON-classes of org.richfaces.json, thx for that tip!

            how can i convert an array to json in javascript. I assumed that richfaces uses normal prototype lib and .toJSON() is provided by every object. but unfortunately this method isn't returning any value but ending in a javascript error.

            How can I convert an JS object to JSON with richfaces?


            btw. this forum search is total crap, i couldn't find my own topic for 15min by using search terms that are all included in the title and message. I finally just browsed the whole forum :( Writing this I suggest that this should be improved or at least a list of "own topics" should be available. thx

            • 3. Re: Mapping javascript array to a bean collection property
              nbelaevski

               

              how can i convert an array to json in javascript. I assumed that richfaces uses normal prototype lib and .toJSON() is provided by every object. but unfortunately this method isn't returning any value but ending in a javascript error.
              The first thing to check is whether prototype.js has been loaded for this page (only the required scripts are loaded if you are using default settings). If it's not there, you can add it using a4jloadScript component.

              btw. this forum search is total crap, i couldn't find my own topic for 15min by using search terms that are all included in the title and message. I finally just browsed the whole forum :( Writing this I suggest that this should be improved or at least a list of "own topics" should be available. thx

              Big upgrade of forum software is planned for May, so we have to wait for this.

              • 4. Re: Mapping javascript array to a bean collection property
                mugglmenzel

                All loaded! PrototypeScript, json-mini and json-dom. toJSON() Method is defined but apparently not working. I use the .toJSON() method on an array that's a clone of another on (array2 = array1.clone();).

                array2 definetly has the correct data set. when testing by doing an alert() on array2 it perfectly shows me its array contents. but trying to alert the .toJSON() of it the scripting dies at that point.

                weird thing! does the richfaces prototype script define the .toJSON() method for every kind of object but not for arrays?

                regards
                Michael

                • 5. Re: Mapping javascript array to a bean collection property
                  mugglmenzel

                  Hmm, just testet a little the .toJSON() method:
                  When I use it on a new defined array consisting of native types it works perfectly.
                  But the array I originally wanted to transform into JSON is filled with GLatLng Objects of the google API. Is the prototype framework not applied to the google api objects?

                  that might cause the problem! how can I JSONize these objects then?

                  regards
                  Michael

                  • 6. Re: Mapping javascript array to a bean collection property
                    mugglmenzel

                    Finally solved!
                    I tried to construct the method GLatLng.prototype.toJSON but this one was never called when I triggered a .toJSON on a GLatLng Object so I finally wrote a function to convert the Array to JSON.
                    The function is quite simple and just creates {lat, lng} objects out of the GLatLng objects and adds them to an array which can be JSONized.

                    Thx for your help!
                    Whoever might need this functionality in his JSF pages can ask me for some code.

                    topic closed.