Talk:AJAX - Return Arrays and Structs to your JavaScript with XML

From DataFlex Wiki
Jump to navigationJump to search

From Akennard:

I just wanted to ask 'why' this works ....

  Get paXml of hoXml to aXml
  Send Destroy of hoRoot
  Send Destroy of hoXML	
  Function_Return aXML


Is it because VDF does not ACTUALLY destroy and objects untill it gets to end_function ? because returning a handle to something you have just destroyed doesn't look quite right ? ie you are returning a handle to nothing or something that might still exist at that point ?

Akennard - try typing "paXML" into the VDF Help Index (that's all I did, using VDF 14.0 Help) and you will get a page which includes the following: "Note: This property allocates memory for the data and it is therefore important that the data be disposed of when you are done. This is done using the Free() function." This is the clue to what is going on: you have an XML object (hoXML is a reference to this) which you later destroy, but calling paXML has a specific (and documented!!!) side effect: "This property allocates memory..." (which, IMO, makes it a function/method, not a property... but then I'm picky!) - and into that memory it then copies the XML of the object it was called on (although the documentation does not explicitly state this, it does clearly imply it), so it is independent of that object and does not disappear when you "Destroy" it, which is why you need to manually "Free()" the memory when you are done.
Hence you are not returning a handle to an object you have destroyed, but a pointer to a lump of memory containing a copy of the XML that was in that object... so not so strange! Wink.jpg
--Mike 15:38, 15 January 2009 (UTC)

VDF has a special XmlHandle return type for returning a hoXML object and the tidying up after itself Is there a reason why this has not been used in this case ? eg would this work for AJAX ?

{ Published = True  }
{ Description = ""  }
Function NameCasing String sName Returns XMLHandle
  Handle hoRoot hoXML hoEvent
  Address aXml
  // create xml document
  Get Create U_cXMLDOMDocument                            to hoXML
  Set pbValidateOnParse of hoXML                          to False
  Get CreateDocumentElement of hoXML "response"          to hoRoot
  Send AddElement of hoRoot "lowercase" (lowercase(sName))
  Send AddElement of hoRoot "uppercase" (Uppercase(sName))	
  Send AddElement of hoRoot "propercase" (Uppercase(Left(sName,1))+lowercase(Right(sName,(length(sName)-1))))	
  Function_Return hoXML  // runtime will transfer and then destroy this object because of XMLHandle return type
End_Function  // NameChanges

I believe using JSon is a much better match.

We have been using JSON to move VDF Structs to Javascript. This is I believe a much nicer and simpler way (and definately faster). We did one test of all customers, with all orders, with all orderlines, from VDF to ASP. On this particular laptop using VDF took 40 seconds, sending the same data using JSon it took 1.2 seconds...

Anyway, no time now to update an article, but just wanted to point this out.