home mail me! syndication

overthinkings

by Ruben Daniels

Dynamically loading jml

This weeks snippet shows how you can fetch some jml (Javeline Markup Language) from the server and run it in your jpf application. This is a good way to dynamically load parts of a big webapp or website.

First we set up the basic calls to retrieve the jml. I’ve added some handling for the error and timeout states to make sure we don’t create errors when the user is disconnected from the internet.

function callback(data, state, extra){
    if (state == jpf.ERROR || state == jpf.TIMEOUT)
        return alert("comm error:" + extra.message);

}

new jpf.http().getXml("somejml.xml", callback);

The final step is to parse the loaded jml data. We use the W3C DOM call, document.createElement to instantiate the element. Javeline Platform allows you to pass jml as a string to this function.

var jmlNode = jpf.document.createElement(data);
jpf.document.documentElement.appendChild(jmlNode);

Together it looks like this:

function callback(data, state, extra){
    if (state == jpf.ERROR || state == jpf.TIMEOUT)
        return alert("comm error:" + extra.message);

    var jmlNode = jpf.document.createElement(data);
    jpf.document.documentElement.appendChild(jmlNode);
}

new jpf.http().getXml("somejml.xml", callback);

It’s that simple.

—–

If you are new to Javeline Platform don’t hesitate to try it out. The new ajax.org website contains the downloads and reference guide which will get your started.

5 Comments »

  Konstantin Lalin wrote @ March 10th, 2009 at 4:22 pm

Didn’t manage to make this piece of code work:

var jmlNode = jpf.document.createElement(data);
jpf.document.documentElement.appendChild(jmlNode);

nor dynamic nodes appear in DOM nor they get drawen.
But alternative code works (data is string of xml):

var xmlNode = jpf.getJmlDocFromString(data).documentElement;
jpf.JmlParser.parseMoreJml(xmlNode, mycontainer.oInt, mycontainer, true);

It’s that simple, but i cannot make things work (
And, by the way, how can i unload the code? Tried mycontainer.destroy(true) but following loading of code doesnot work

  Ruben wrote @ March 10th, 2009 at 5:46 pm

Hi Konstantin,

Which version of JPF are you using? Could you sent to the mailinglist what your data string is. It’s important to not have a j:application tag around the jml, but just a single root element. For instance an j:tree. It might help to use the latest nightly.

The method you are using is not bad, although it might be less easy to remember.

You can use the destroy() method to completely remove an element. You could also use either jmlElement.removeNode() or jmlElement.parentNode.removeChild(jmlElement). But those functions keep references to the element in order to reattach it. If you can’t get destroy to work please sent a test case to the mailinglist and it will be investigated by me or another member of the community.

Kind Regards,
Ruben

  Konstantin Lalin wrote @ March 11th, 2009 at 10:46 am

Thank you for response! Removing j:application helped, but anyway appendChild throws an exception. I’ll write to maillist.

  Martijn wrote @ June 30th, 2009 at 3:51 pm

I’m trying to get the example working, but nothing happens. I’m using “jpf_debug.js”. Is my assumption correct that this example is written for an older version, i.e. “javeline_platform.js”?

  Ruben wrote @ June 30th, 2009 at 4:22 pm

Hi Martijn,

Yes, you are assuming correctly. In the latest version (from SVN) you can do this:

someExistingJmlNode.replaceJml(”url:someloc/filename.xml”);

And that’s all there is to it.

Ruben

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>