Wednesday, July 02, 2008

Reusable Module Components in APEX

When building a serious application you often need to repeat certain regions in a couple of pages, for instance customer or order information. In APEX you can ofcourse copy a region to another page, but that enhances your maintenance effort when changes are needed on this copied region. Another solution is to group all the pages that share this region in one application and define the region on the famous Page 0. But maybe there is a more elegant way to solve this....

As an example I use the 'My Favorite Tasks' region I described in the previous post. First we set the page alias to 'MYFAV', to circumvent a static reference to a page number. In the region header of 'My Favorite Tasks' we set a HTML tag '<snap>' and '</snap>' in the footer (this <snap> is just an example, it can be anything, as long as it isn't a regular HTML tag). Now define a new HTML region on the page where you need to 'reference' this 'My Favorite Tasks' region and set the region source to <div id='Favorites'></div>. Now we created a placeholder for future use...

Now let's fill this placeholder with the contents of the 'My Favorite Tasks' region.
Set the Page HMTL Body Attribute to : onload="ShowFavs();". This will fire a javascript function when the page is loaded. You probably have to set the value of 'Cursor Focus' to 'Do not focus cursor' otherwise you'll get a 'You may not declaratively set cursor focus' error. Next define the javascript function in the HTML Header section.

<script type="text/javascript">
function ShowFavs(){
var get = new htmldb_Get(null,$v('pFlowId'),null,'MYFAV');
gReturn = get.get(null,'<snap>','</snap>');
get = null;

This function will request the 'MYFAV' page. Then the page is 'stripped' so only the HTML between the tags is returned. And this HTML is rendered in the placeholder...

So with this simple and elegant solution you can define 'region building blocks' (even more meaningful than 'My Favorite Tasks') and assemble pages using this building blocks! So re usability increases and your maintenance effort decreases.

In a next post I will show how you even can put regions anywhere on a page without defining a placeholder at all...
Post a Comment