This example demonstrates how to refresh an RDF datasource from JavaScript; for example, because its contents are dynamic and may change over time. The example is implemented with a signed script, and should run within a Mozilla-based browser if you install this object-signing certificate. (Note: you should understand that doing so will grant the script privileges that are potentially dangerous.)
Click here after you've installed the certificate to run the sample.
The sample's source code (you may need to “view source” to actually see it) loads RDF/XML from a CGI script that generates a new value about once a second. Here is the relevant script from the sample:
// Grab the RDF service; we'll be needing it later.
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
.getService(Components.interfaces.nsIRDFService);
var vbox;
var datasource;
function init() {
// After the XUL is loaded, load RDF/XML from the CGI script and
// populate our template using that data. This is called once,
// from the XUL document's "onload" handler.
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
datasource = RDF.GetDataSource("http://www.maubi.net/~waterson/cgi-bin/spew.cgi");
vbox = document.getElementById("vbox");
vbox.database.AddDataSource(datasource);
}
function reload()
{
// Force the data to be reloaded from the CGI. Called when the
// user clicks the "Reload" button.
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
datasource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource).Refresh(false);
}
The init function is called from the XUL document's
onload handler. It uses the RDF service to load RDF/XML
data from spew.cgi, and installs the datasource into the
<vbox> template. The reload
function is called each time you click the Reload
button: it simply invokes the
nsIRDFRemoteDataSource::Refresh method. The
false argument indicates that the refresh should be
performed asynchronously. The enablePrivilege calls
are necessary for the script to run properly from an HTTP URL.
These would not be necessary if your script was running as chrome.
The CGI spew.cgi is as follows:
#!/usr/bin/perl -w
print qq{Content-type: text/rdf
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:nc="http://home.netscape.com/NC-rdf#">
<rdf:Description about="urn:root">
<nc:links>
<rdf:Seq>
};
print " <rdf:li><rdf:Description nc:name=\"" . time() . "\" /></rdf:li>\n";
print qq{ </rdf:Seq>
</nc:links>
</rdf:Description>
</rdf:RDF>
};
It simply spits out some RDF/XML, using Perl's time
function to generate a new value for the nc:name
property.