HTML Post Loading and Processing Resources using AJAX – Part 3: multiple, dependent resources and custom processing

0

 AJAX opens up a lot of possibilities – that in truth by and large
already existed pre-AJAX. In a series of articles, I am discussing the
concept of post loading resources: after the HTML document is loaded,
additional resources can be retrieved from various sources – both local
and remote – and processed in a plethora of different ways. The concept
of Post Loading Resources was introduced in the first installment Ajax-based Post Loading of resources in HTML pages – for reuse of resources and fast user feedback.
The second article in the series, HTML Post Loading Resources Framework (AJAX Based) – Part 2 – Loading and pasting simple content, presented a
simple JavaScript library that implemented the basics of this concept.
It loaded multiple resources, both local and remote, and processed them
by pasting them into the value property of Forms items or the InnerHtml
property of Container elements such as DIV and TD.

This
article introduces a little more complexity: how to specify a custom
post processor for a Post Loaded Resource – for example to execute a
post loaded piece of Java Script or to scrape HTML content from a
remote Web Page – and how to define dependencies between Post Loaded
Resources to ensure that processing only takes place when all required
resources are available. The source code for this article can be
downloaded in the Resources section.....

Defining a Custom Processor

Allowing
custom processors for Post Loaded Resources is extremely simple.
Thusfar, we had built into our framework a standard processor function
processPostLoad() called from the ContentLoader object when the
Repsonse was successfully received. We will not change this, but we
will add a new property processor to the PostLoadResource
object and extend the processPostLoad() function – or actually its
helper startProcessing() – to deal with this property.

First a change in the instantiation of the PLR object:

amis.PostLoadResource=function           <br />                   ( elementId<br />                   , url<br />                   , requireProxy // boolean indicating whether or not the resource must be acquired through a proxy from a remote domain<br />                   , label<br />                   , processor // function reference of function that will process the resource when received<br />                   , dependsOn // other PostLoadResources this PLR may depend on, such as an XSLT that an XML depends on for being processed<br />                   ) {<br />  this.elementId=elementId;<br />  this.url=url;<br />  this.requireProxy = requireProxy;<br />  this.state = amis.STATE_UNINITIALIZED;<br />  this.label = label;<br />  this.req = null;<br />  this.id = null;<br />  this.processor= processor;<br />}<br /><br />  // create a new PLR object and add it to the array of PLR objects to be dealt with when the page has loaded<br />  function addPostLoadResource<br />           ( elementId<br />           , url<br />           , requireProxy // boolean indicating whether or not the resource must be acquired through a proxy from a remote domain<br />           , label<br />           , processor // function reference of function that will process the resource when received<br />           ) {<br />     var plr = new amis.PostLoadResource(elementId, url, requireProxy,label, processor);<br />     var size = postLoadResources.push( plr ); // add a new PostLoadResource object to the array<br />     plr.id = size -1; // ensure the plr objects knows where it sits in the postLoadResources array<br />     return plr;<br />  }<br /><br /><br />

The second change that we need for a custom processor is in the startProcessing() function:

  function startProcessing(postLoadResource) {<br />    if (postLoadResource.processor) <br />    { <br />      postLoadResource.processor.call(postLoadResource);<br />      }<br />    else {<br />      if (postLoadResource.elementId) {<br />        // go find element and load contents<br />        var element = el(postLoadResource.elementId);<br />        if (element.value) {<br />          element.value = postLoadResource.req.responseText;<br />        }<br />        else {<br />          element.innerHTML = postLoadResource.req.responseText;<br />        }<br />      }<br />    }// processor<br />    postLoadResource.state = amis.STATE_PROCESSED;<br />  }// startProcessing<br />&nbsp;

It is a simple change: the function checks whether the processor
property has a value. If so, it is assumed to be function reference and
that function is invoked, with the current postLoadResource object as
context. The latter means that the custom processor can refer to the
PLR object through the this reference. We see how that is done in this
sample custom processor; it takes the responseXml object, retrieves all
ROW/DEPARTMENT elements and prints them as list items to the DIV
container:

&nbsp;   function customDeptProcessor() {<br />    var postLoadResource = this;<br />    var depts = postLoadResource.req.responseXML.getElementsByTagName(&quot;ROW&quot;);<br />    var newContent =&quot;&lt;H3&gt;The list of Departments&lt;/H3&gt;&lt;ul&gt;&quot;;<br />    for (var i=0; i&lt;depts.length; i++) {<br />      var dept = depts[i];<br />      var dname = dept.getElementsByTagName(&quot;DNAME&quot;)[0].firstChild.nodeValue;<br />      var loc = dept.getElementsByTagName(&quot;LOC&quot;)[0].firstChild.nodeValue;<br />      newContent = newContent+&quot;&lt;li&gt;&quot;+dname+&quot; (in &quot;+loc+&quot;)&lt;/li&gt;&quot;;<br />    }//for<br /><br />    el(postLoadResource.elementId).innerHTML=newContent+&quot;&lt;/ul&gt;&quot;;<br />  }// customDeptProcessor<br /><br />

The XML Resource dept.xml that is processed here looks like:

 

 
To
instruct the PLR library to post load and process the dept.xml
resource, the following snippet is required in the HTML document:

    &lt;DIV style=&quot;background-color :yellow&quot;&gt;<br />      &lt;pre id=&quot;PL1&quot;&gt;<br />        &lt;Script language=&quot;JavaScript&quot;&gt;    <br />          addPostLoadResource('PL1', 'dept.xml', false, 'POSTLOAD_DEPARTMENTS',customDeptProcessor);<br />        &lt;/Script&gt;<br />      &lt;/pre&gt;<br />    &lt;/DIV&gt;<br />&nbsp;

The result of this custom processor is shown below, in the yellow box. 

 

Example of Custom Post Processor: HTML Scraping

The second container in the screenshot above displays the actual headlines from the popular website www.theserverside.com.
Since these headlines are also available as an RSS feed, the example is
slightly contrived, but instructive nevertheless. In this case, we have
specified http://www.theserverside.com/index.tss
as our resource-to-be-post-loaded. We have also setup a custom
processor that knows the (current) structure of the home page on TSS
and is capable of getting the headlines out of the HTML page and into a
piece of our own HTML that we paste in the innerHtml property of our
DIV. This is a fairly simple way of putting together a Portal on the
world: just grab and scrape interesting portions of good websites. On a
moral note: you are using their content without absorbing their
commercial messages, which can perhaps be seen as a form of cheating or
worse. That is for your own conscience to decide.

The custom processor that takes the HTML from the TSS and ‘srcapes’ it, looks like this:

  function customProcessTSS() {<br />    var postLoadResource = this;<br />    // look for the first 10 H1 elements<br />    var pos=-1;<br />    var endpos=-1;<br />    var i=0;<br />    var newHtml=&quot;&quot;;<br />    for (i=0;i&lt;10;i++) {<br />      pos = postLoadResource.req.responseText.indexOf('&lt;h1&gt;', pos+1);<br />      endpos = postLoadResource.req.responseText.indexOf('&lt;/h1&gt;', pos+1);<br />      newHtml = newHtml + postLoadResource.req.responseText. substring(pos, endpos+5);<br />      // replace local, relative reference within TSS with full URL path<br />      newHtml= newHtml.replace('&lt;a href=&quot;/news/','&lt;a target=&quot;_blank&quot; href=&quot;http://www.theserverside.com/news/');<br />    }//for<br />    el(postLoadResource.elementId).innerHTML='&lt;H2&gt;TSS New Entries&lt;/h2&gt;'+newHtml;<br />  }// customProcessTSS<br /><br />

It
is fairly basic and not overly robust. It depends on the fact that the
Headlines on TSS are wrapped inside H1 elements – and no other
information on the page is. So it just locates an H1 element, finds the
associated </H1> tag and uses everything in between as the
content to scrape. It turns out that the references to the actual news
stories are local references, not starting with http, but starting with
a relative reference /news/. In order to maintain the reference, we
have to change the relative reference into an absolute one, pointing to
the website of TSS. Finally, we get hold of the target element
specified in the PLR object and paste the list of scraped headlines
into it.

To include these post loaded headlines in our HTML document, we include the following fragment:

    &lt;h3&gt;Download the frontpage of The Server Side and extract the headlines&lt;/h3&gt;<br />    &lt;Script language=&quot;JavaScript&quot;&gt;    <br />      addPostLoadResource('PL2', 'www.theserverside.com/index.tss', true,  'POSTLOAD_TSS_LOADER', customProcessTSS);<br />    &lt;/Script&gt;<br />    &lt;DIV id=&quot;PL2&quot; &gt;<br />    &lt;/DIV&gt;<br />&nbsp;

Processing Post Loaded JavaScript Resources

One
of the ways in which a post loaded resource can be processed is by
executing it. The JavaScript eval() operator allows us to pass in a
string – or the responseText for a post loaded resource – and have it
executed. This allows us to add additional JavaScript functions and
data structures to our HTML document after it has been loaded. In an
earlier post I mused over ways to work with large volumes of data
cached on the client machine in ‘static’ JavaScript libraries in
combination with lightweight cache updates. Being able to post load a
JavaScript resource and execute it on the fly is probably the answer to
that challenge. See Increasing speed of making data available in the web-page – on AJAX, controlling data caching and JavaScript Libraries.

A very simple example of a custom processor that processes the Post Loaded Resoiurce as JavaScript would be something like:

  // this function treats the req.responseText as a piece of JavaScript<br />  // the responseText is evaluated : this can do several things like<br />  // creating new functions, adding data , calling already pre-loaded<br />  // JavaScript objects; this shows we can pull in additional JavaScript<br />  // libraries in postload operations - from other domains as well as <br />  // our own!!<br />  function customJSProcessPostLoad() {<br />    var postLoadResource = this;<br />    eval(postLoadResource.req.responseText);<br />  }<br /><br />

Dealing with dependencies between Post Loaded Resources

The
last challenge we will tackle in this article is how to link PLR
objects to one another. It happens regularly that a certain resource
can only be processed when another resource is available. For example
the processing of Resource A must be done by a JavaScript function that
will be loaded as part of Resource B. Or Resource A is and XML document
and Resource B the XSLT stylesheet that is needed to transform it (see
next installment for XSLT transformations). So we need a way to tell
the PLR framework that a resource can be loaded alright but can only be
processed when resources it depends upon have been loaded. We will
implement this following a well known Design Pattern: the Observer.

First
we extend the Post Load Resource object with two properties: an Array
with PLR objects the object depends upon (the observed) and an Array
with PLR objects that depend on it (the observers).

/*--- content loader object for cross-browser requests ---*/<br />amis.PostLoadResource=function           <br />                   ( elementId<br />                   , url<br />                   , requireProxy // boolean indicating whether or not the resource must be acquired through a proxy from a remote domain<br />                   , label<br />                   , processor // function reference of function that will process the resource when received<br />                   , dependsOn // other PostLoadResources this PLR may depend on, such as an XSLT that an XML depends on for being processed<br />                   ) {<br />  this.elementId=elementId;<br />  this.url=url;<br />  this.requireProxy = requireProxy;<br />  this.state = amis.STATE_UNINITIALIZED;<br />  this.label = label;<br />  this.req = null;<br />  this.id = null;<br />  this.processor= processor;<br />  this.dependsOn = dependsOn;<br />  this.dependents = new Array();<br />  // also tell every PLR that is depended on to notify this PLR when they are processed<br />  if (dependsOn) {<br />    var i=0;<br />    for (i=0;i&lt;dependsOn.length;i++) {<br />      dependsOn[i].dependents.push(this);<br />    }//for<br />  } // if dependsOn<br />}<br /><br />  // create a new PLR object and add it to the array of PLR objects to be dealt with when the page has loaded<br />  function addPostLoadResource<br />           ( elementId<br />           , url<br />           , requireProxy // boolean indicating whether or not the resource must be acquired through a proxy from a remote domain<br />           , label<br />           , processor // function reference of function that will process the resource when received<br />           , dependsOn // an Array of PLR objects on which the newly added PLR depends<br />           ) {<br />     var plr = new amis.PostLoadResource(elementId, url, requireProxy,label, processor, dependsOn);<br />     var size = postLoadResources.push( plr ); // add a new PostLoadResource object to the array<br />     plr.id = size -1; // ensure the plr objects knows where it sits in the postLoadResources array<br />     return plr;<br />  }<br /><br />

The
depensOn parameter holds an Array that contains PLR objects on which
the new Post Load Resource depends. During creation of the PLR object,
this array is iterated over and every PLR on which a dependency exists
is informed of the fact that the new PLR depends on – listens to or
observes – the PLR it depends on. Note that it is very low level,
unencapsulated way of registering listeners or observers on an
Observable. Here is definitely some room for refactoring.

The
required logic is created in the processPostLoad() function that is
invoked whenever the response for a PLR is succesfully received by the
ContentLoader object. This function will no longer automatically start
processing the PLR, it will check first whether it has any observers -
any PLRs that depend on it. If so, these dependent PLRs are notified of
the fact that this PLR is loaded – and perhaps they can now start
processing themselves. Next, processPostLoad() looks if the PLR has any
dependencies itself on other PLRs and if so it will verify whether
those PLRs are already in. Only when they are all loaded can processing
of this PLR proceed. If not all dependencies are satsified, this PLR is
not processed but put on hold – state = amis.STATE_LOADED_AND_WAITING.
The processPostLoad() function used the dependsOnUnloaded() helper to
find out whether any dependencies exist on PLRs that have not yet been
loaded.

  // this function checks whether the PLR has dependencies on other PLRs<br />  // that are not yet loaded<br />  function dependsOnUnloaded( postLoadResource) {<br />    // check if the postLoadResource depends on other plrs that are not yet loaded<br />    // if so, it goes into the waiting room - state= amis.STATE_LOADED_AND_WAITING<br />    if (postLoadResource.dependsOn) {<br />      var i=0;<br />      for (i=0;i&lt;postLoadResource.dependsOn.length;i++) {<br />        if (postLoadResource.dependsOn[i].state &lt;  amis.STATE_LOADED_AND_PROCESSING) {<br />          return true;<br />        }<br />      }//for<br />    } // if dependsOn<br />    return false;<br />  }//dependsOnUnloaded<br /><br /><br />  // This function is called by the ContentLoader when the AjaxRequest is successfully completed<br />  // and the requested content is available for further processing.<br />  function processPostLoad() {<br />    var postLoadResource = this.parameter;<br />    postLoadResource.req = this.req;<br />    postLoadResource.state = amis.STATE_LOADED_AND_PROCESSING;<br />    // now tell all dependents that this plr is being processed<br />    if (postLoadResource.dependents) {<br />      var i=0;<br />      for (i=0;i&lt;postLoadResource.dependents.length;i++) {<br />        postLoadResource.dependents[i].notify(postLoadResource);<br />      }//for<br />    } // if dependents<br />    // check if the postLoadResource depends on other plrs that are not yet processed<br />    // if so, it goes into the waiting room - state= amis.STATE_LOADED_AND_WAITING<br />    if (dependsOnUnloaded( postLoadResource)) { <br />       postLoadResource.state = amis.STATE_LOADED_AND_WAITING;<br />       return;<br />    }<br />    startProcessing( postLoadResource);<br />  }// processPostLoad<br /><br />

The notify function that is invoked by processPostLoad is defined on the PLR object, on the prototype itself:

&nbsp;// this function is to be called for a PLR that depends on another PLR when<br />// that other PLR has been loaded so it (the listener or observer) can evaluate <br />// whether all its dependencies are available and so it can start processing itself<br />amis.PostLoadResource.prototype.notify=function(plr){<br />  if (this.state == amis.STATE_LOADED_AND_WAITING) {<br />    startProcessing(this);<br />  }<br />}<br /><br />

The
only piece missing is an example of the JavaScript call we will now use
to specify dependencies between Post Load Resources. The third example
in the screenshot above shows two Select elements, with data on
Departments and Employees. However, the HTML document only contains
this snippet:

&nbsp;    &lt;h3&gt;Postload an HTML fragment (creating select items) and a dependent JS library (that loads the data)&lt;/h3&gt;<br />      &lt;Script language=&quot;JavaScript&quot;&gt;    <br />        var empdeptlistsPLR = addPostLoadResource('PL_9', 'EmpDeptLists.txt', false,  'POSTLOAD_EMPDEPTLISTS_LOADER');<br />        var jsempdeptPLR = addPostLoadResource(null, 'EmpDeptListManager.js', false, 'JS_FOR_EMPDEPT_LOADER', customJSProcessPostLoad,  new Array(empdeptlistsPLR));  <br />     &lt;/Script&gt;<br />    &lt;DIV id=&quot;PL_9&quot; style=&quot;background-color :pink&quot;&gt;<br />    &lt;/DIV&gt;<br /><br />

The
SELECT elements are post loaded from a local resource – a text file -
that holds some text that we can paste as HTML into the DIVs innerHtml
property. The data is post loaded from XML files, dept.xml and
emp_<deptno>.xml. However, the JavaScript that post loads the
data for the SELECT elements must first be post loaded itself. What we
see in the HTML snippet above is that two resources are post loaded:
the EmpDeptLists.txt resource, that contains a bit of HTML setting up
the SELECT items, and the EmpDeptListManager.js resource which is a
JavaScript library that will go on to post load the department data and
populate SELECT element with it; it also defined the onChange event
handler on the Department list that will populate the Employees list
based on the Department chosen. Before this JavaScript library can do
its work, the HTML elements on which it acts must be available. So we
need to make sure that the jsempdeptPLR is only processed when the
empdeptlistsPLR has been loaded. We do this by including the new
Array(empdeptlistsPLR) in our call to addPostLoadResource. The
dependency facilities that we added to the PLR framework above take
care of only processing the JavaScript in EmpDeptListManager.js when
the empdeptlistsPLR is in. Note: we have a weak spot here: in this
particular case, it is not good enough that the empdeptlistsPLR is in:
it must have been processed as well! Another item on the TODO list.

Turning
our attention to the contents of the JavaScript library that is loaded
in. It contains a number of functions that will help process the
Department data that we will load, react to another selected Department
by loading the corresponding Employee data into the Employees List etc.
It also contains some JavaScript statements that are immediately
executed:

  // execute immediately after loading this piece of JavaScript<br />  // find the dept element (a select) and add an onChange event handler: the function loadEmployees<br />  document.getElementById(&quot;dept&quot;).onchange=loadEmployees;<br /><br />  // create a new PostLoadResource object for the dept.xml document with department data<br />  var deptPLR = addPostLoadResource(null, 'dept.xml', false, 'POSTLOAD_EMPDEPT_LOADER', deptProcessor);<br />  // kick it off to get the Departments asap<br />  postLoadSingleResource(deptPLR);<br /><br />

First
we get hold of the dept item, the Department SELECT that is created
when the PLR EmpDeptLists.txt was loaded and processed. Then we assign
to this item an onChange event handler. The handler is a function
called loadEmployees(); it is also defined in this library; when a new
Department is selected, loadEmployees() is invoked. It creates a new
PLR object and has it processed by the PLR framework:

  function loadEmployees()  {<br />    var deptList = document.getElementById(&quot;dept&quot;);	<br />    clearList(document.getElementById(&quot;emp&quot;));<br />    // for each department there is a file called emp_&lt;number of department&gt; to hold the employees<br />    var file = &quot;emp_&quot;+ deptList.value +&quot;.xml&quot;<br />    // define a new PostLoadResource<br />    var empPLR = addPostLoadResource(null, file, false, 'POSTLOAD_EMP_LOADER', empProcessor);<br />    // and immediately kick it off<br />    postLoadSingleResource(empPLR);<br />  }<br />&nbsp;

We create a new PLR object for the dept.xml resource that contains the
Department data that we want to use for populating the Department list.
Since the HTML document is already loaded and therefore the onLoad
event that called the goPostLoadResources() function will not take
place again, we have to start processing of this new PLR ourselves,
hence the call to postLoadSingleResource. We specify a custom processor
called deptProcessor. This is a reference to a function that is loaded
as part of this JavaScript library:

<br />  /**<br />   * Populate the list with the data from the request<br />   * (Could be done in a generic manner depending of the XML...)<br />   */<br />  function deptProcessor() { <br />    var postLoadResource = this;<br />    var list = document.getElementById(&quot;dept&quot;);	<br />    clearList(list);<br />    addElementToList(list, &quot;--&quot;, &quot;Choose a Department&quot; );<br />    var items = postLoadResource.req.responseXML.getElementsByTagName(&quot;ROW&quot;);<br />    for (var i=0; i&lt;items.length; i++) {<br />      var node = items[i];<br />      var deptno = node.getElementsByTagName(&quot;DEPTNO&quot;)[0].firstChild.nodeValue;<br />      var dname = node.getElementsByTagName(&quot;DNAME&quot;)[0].firstChild.nodeValue;<br />      var loc = node.getElementsByTagName(&quot;LOC&quot;)[0].firstChild.nodeValue;<br />      addElementToList(list, deptno, dname  );<br />    }//for<br />  }// deptProcessor<br /><br />  /**<br />   * remove the content of te list<br />   */<br />  function clearList(list) {<br />  	while (list.length &gt; 0)<br />  	{<br />      list.remove(0);<br />    }<br />  }<br /><br />  /**<br />   * Add a new element to a selection list<br />   */<br />  function addElementToList(list, value, label)<br />  {<br />    var option = document.createElement(&quot;option&quot;);<br />    option.value = value;<br />    var labelNode = document.createTextNode(label);<br />    option.appendChild(labelNode   );<br />    list.appendChild(option);<br />  }<br /><br />

When
the deptProcessor has done its job, the Department Select is populated
with Department data. When we pick a Department, the onChange event is
fired, invoking the loadEmployees() function that creates a new PLR to
load the relevant employee data that will be processed by the
empProcessor() when received. The empProcess() populates the Employee
List:

 

One
last element is to be discussed: the empProcessor function that is
called to process the results when the empPLR that was created when a
new Department was selected is ready for for action. It is quite
similar to the deptProcessor function.

  function empProcessor() {<br />    var postLoadResource = this;<br />    var list = document.getElementById(&quot;emp&quot;);	<br />    var items = postLoadResource.req.responseXML.getElementsByTagName(&quot;ROW&quot;);<br />    clearList(list);<br />    if (items.length &gt; 0) {<br />      for (var i=0; i&lt;items.length; i++) {<br />        var node = items[i];<br />        var empno = node.getElementsByTagName(&quot;EMPNO&quot;)[0].firstChild.nodeValue;<br />        var ename = node.getElementsByTagName(&quot;ENAME&quot;)[0].firstChild.nodeValue;<br />        addElementToList(list, empno, ename  );<br />      }//for<br />    }<br />    else<br />    {<br />     alert(&quot;No Employee for this department&quot;);<br />    }<br />  }// empProcessor<br />&nbsp;

If we select another Department, new data is loaded and used to populate the list:

 

Resources

Download the sources for this article PostLoadResourceDemo2.zip

The installments in this series are

 

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Comments are closed.