gal007
gal007

Reputation: 7202

Accessing the XForms/XSLTForms model instances with javascript

I'm trying -uncessfully- to access the instances of my model, in order to add some data via Js. I know I can do that by adding a trigger with an insert action, but I'm working with some canvas so I really need to do the same via Js.

I added a onLoad function that is triggered when the document body is already loaded. I tryed to get the instance with the "instance" action of xforms, but it is not recognized as a function.

var instanceElement = instance('application');
console.log( instanceElement);

I also tryed to get the instance with:

document.getElementById(id);

But it returned null. So, how can access the model? Thanks in advance!

Upvotes: 0

Views: 338

Answers (2)

Bill Velasquez
Bill Velasquez

Reputation: 893

Calling directly XSLTForms code can bring mantainance problems, because sometimes object names change.

A good idea is creating wrappers functions to encapsulate common XSLTForms tasks.

Here are some of them that you can use:

function xf_getInstance(modelId, instanceId)
{
    var model = window.document.getElementById(modelId);
    var doc = model.getInstanceDocument(instanceId);
    return doc;
}

function xf_getNode(context, path)
{
    return XsltForms_browser.selectSingleNode(path, context);
}

function xf_changeNode(node, value)
{
    XsltForms_globals.openAction("XsltForms_change");
    XsltForms_browser.setValue(node, value || "");
    document.getElementById(XsltForms_browser.getMeta(node.ownerDocument.documentElement, "model")).xfElement.addChange(node);
    XsltForms_browser.debugConsole.write("Setvalue " + node.nodeName + " = " + value);
    XsltForms_globals.closeAction("XsltForms_change");
}

function xf_fireEvent(targetId, eventName)
{
    XsltForms_globals.openAction("XsltForms_dispatch");
    XsltForms_xmlevents.dispatch(document.getElementById(targetId), eventName);
    XsltForms_globals.closeAction("XsltForms_dispatch"); 
}

Upvotes: 3

gal007
gal007

Reputation: 7202

var model = window.document.getElementById("model-id");
var ins = model.getInstanceDocument("instance-id");
console.log(ins.documentElement.textContent);

Upvotes: 0

Related Questions