Servoy: make JavaScript object representation of record

I got asked for an example on the Servoy forum of how to make a JavaScript object that is essentially a clone of a "record" object in Servoy, which is actually a Java host object that's tied to a foundset and has some limitations and overhead associated with it. So, here's how simple it is...

JavaScript:
  1. var rec = // get your record from somewhere
  2. var obj = new Object();
  3.  
  4. for(var item in rec)
  5. {
  6.    obj[item] = rec[item];
  7. }

After that, you have an object with all the same named properties and values at the fields in the record's table. Of course, it is linked to nothing, so if you choose to modify those values, they won't make it back to the database -- but it's a handy thing to do now and then for read-only type data you might want to load into a global at startup.

Leave a Reply

Switch to our mobile site