Rather than tearing down the rendered xform instance, reloading, and simulating a DOM-ready event, have you considered loading the xforms within an iframe? That would be the least fraught way of dynamically loading/switching an xform.
If you don't want a scrollable iframe and wish to make it appear as if the form is loading onto the page itself, you could dynamically resize the height of the iframe element to give the appearance that it's a normal block element (a quick google found this jquery plugin <http://www.lost-in-code.com/programming/jquery-auto-iframe-height/>).
Regarding your second question: if I remember correctly, an xforms submission doesn't cause a reload of the containing document by default (if you set the <submission> replace="all" it will replace the entire page with the contents of the submission response, but it defaults to replace="instance" if unspecified).
If you wish to do some additional javascript processing after a submission completes, you can attach to the "xforms-submit-done" event. The most common way of doing this is to use an <action> element inside the <submission>:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
<script type="text/javascript">
// <![CDATA[
function processSubmit() {
// ajaxy stuff
}
// ]]>
</script>
</head>
<body>
<xf:model id="my-model">
<xf:instance id="main-instance">
...
</xf:instance>
<xf:submission id="submit" method="..." ...>
<xf:action ev:event="xforms-submit-done">
<xf:load resource="javascript:processSubmit()"></xf:load>
</xf:action>
</xf:submission>
</xf:model>
...
</body>
</html>
Alternatively, you can do the same thing as the <action> above using javascript by attaching a handler to the xforms model element on the 'xforms-submit-done' event.
Best,
Frankie Dintino
> --
> You received this message because you are subscribed to the Google Groups "Ubiquity XForms Developers (Users)" group.
> To post to this group, send email to ubiquit...@googlegroups.com.
> To unsubscribe from this group, send email to ubiquity-xfor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ubiquity-xforms?hl=en.
>
I also discovered a bug while drawing up this example, so until that's fixed make sure that your "response" instance has at least one child node (a space between the instance tags will do).
<html>
<head>
<script type="text/javascript">/* <![CDATA[ */
function getInstanceDoc(instanceName) {
var model;
var isXHTML = (document.xmlVersion || (document.contentType && document.contentType === "application/xhtml+xml"));
if (isXHTML) {
model = document.getElementsByTagNameNS("http://www.w3.org/2002/xforms", "model")[0];
} else {
model = document.getElementsByTagName("xf:model")[0];
}
var instance = model.getInstanceDocument(instanceName);
return instance;
}
function submitDoneText() {
var responseDoc, responseText;
responseDoc = getInstanceDoc('response');
// Grab the nodeValue of the instance's root element
if (responseDoc.documentElement && responseDoc.documentElement.firstChild) {
responseText = responseDoc.documentElement.firstChild.nodeValue;
}
}
function submitDoneXml() {
var responseDoc = getInstanceDoc('response');
// We're done!
}
/* ]]> */</script>
</head>
<body>
<xf:model xmlns:xf="http://www.w3.org/2002/xforms" id="xforms-model">
<xf:instance id="main">
...
</xf:instance>
<xf:instance id="response"> </xf:instance>
<xf:submission id="submit-w-xml-response"
resource="..."
ref="instance('main')"
replace="instance"
targetref="instance('response')">
<xf:action ev:event="xforms-submit-done">
<xf:load resource="javascript:submitDoneXml()"></xf:load>
</xf:action>
</xf:submission>
<xf:submission id="submit-w-txt-response"
resource="..."
ref="instance('main')"
replace="text"
targetref="instance('response')">
<xf:action ev:event="xforms-submit-done">
<xf:load resource="javascript:submitDoneText()"></xf:load>
</xf:action>
</xf:submission>
</xf:model>
...
</body>
</html>