1 Reply Latest reply on Jan 18, 2018 2:59 AM by eoinbyrne

    Read/Write Resource element not using a file

    carl@pair-a-links.com Expert

      Page 18 of the vRO Coding Guide (ver 1.0) says


      Possibly use a CompositeType, or if it is really meant for internal use then you can save the Properties object as a Resource Element


      I cannot find any way to write a JS object into a Resource Element.  Nor for that matter, to read it back as an object.  I find lots of ways to read/write files to a Resource Element, just not objects as the coding guide states.  Can anyone help me?



      Carl L.


      Oh, and if anyone is interested, the coding guide is here:


        • 1. Re: Read/Write Resource element not using a file
          eoinbyrne Hot Shot

          A resource element stores a MimeAttachment which contains the content. You can access the actual content value of a ResourceElement like this




          You can write to the resource element like this


          var newResourceContent = "my new resource content";
          var mime = resourceElement.getContentAsMimeAttachment();
          mime.content = newResourceContent;

          To store a custom object into the resource element you can convert it to a JSON string using something like the following


          var myObject = {}; 
          myObject.name = "myObject"; 
          var child = {}; 
          child.name = "child"; 
          myObject.childrenArray= [];  
          var myJSONStringRepresentation = JSON.stringify(myObject);  
          //Note that JSON.stringify may not always produce useful results - it depends on the object type. I generally avoid using it for any built-ins
          //or plugin types as you will often get only the string [object Object] as the result


          I use this mechanism a lot to store small amounts of cached data in vRO for different purposes. The benefit of using a JSON string here is that if you set the MimeType to text/plain then it will be readable in the vRO Client + you can edit it externally if you need to.


          Hope this helps