Singleton and Global Hooks
I have looked that the information about Singleton Design Patterns and subsequently at the GlobalHooks class Ian gave to me, with a little help from a Java-Programmer friend to explain how the Singleton works in principle.
From what I can gather, the GlobalHooks class that Ian has written, is basically a kind of wrapper. By adding the PagesDB to the GlobalHooks as a global property it enables me to then call the database via GlobalHooks, and ensures there is only one database.
However, in the current file structure it is not a true singleton because it doesn't prevent PagesDB from being called directly from elsewhere. If the PagesDB class was implemented as a Singleton, this would prevent it being called from anywhere else, and it could only ever exist as one instance.
I thought I would first try to implement the PagesDB as a Singleton, however, this led to numerous errors, one after another, some of which related to the current file structure and the fact that there is a circular reference between PagesDB and PagesXMLProcessor. It started to become very complex and so I reverted to using the GlobalHooks class as the singleton, and importing the database via the GlobalHooks class.
I added the following code to the main timeline in ebooks to make PagesDB a property of the GlobalHooks class.
//import the database as before to the main timeline
var db:PagesDB = new PagesDB();
//get it to load data
db.loadData(xmlMan.getRoot());
//store database in as property of GlobalHooks
var sys:GlobalHooks = GlobalHooks.getInstance();
sys.addProp("pagesDatabase", db);
Then I added the following code into the pageviewer timeline to call the database stored in the global hooks instance:
var sys:GlobalHooks = GlobalHooks.getInstance();
var db:PagesDB = sys.getProp("pagesDatabase");
I also added a trace to make sure the correct database was loading.
The only negative at the moment is that I removed other code so that the separate swfs don't work on their own because they need to reference the main timeline of the framework. However, I should be able to overcome this with an if statment to direct the db to load directly if it is being played outside the main timeline, but to load global hooks if played from within.
Next steps...modular application design framework, then begin construction all over again.