# Creation of DFrames: Separate phases for Instanciation and Creation

## Definitions

DFrame is a framework in which will fit a HTML page. The characteristics of this framework are specified at the time of its instantiation, by the used Style.

This framework can receive very different kind of pages and it will then often be necessary to customize the DFrame.

The best way of coding is thus to manage instantiations and creations in distinct procedures, the procedure of instantiation being common to all similar DFrames. As for the decoration it will be it distributed in the phases of instantiation for common decoration and creation for specificities of each DFrame:

## Example.

function createDFrameForPage1() {

dFrame1 = instantiateDFrame()

dFrame1.addButton('Action for Page 1', 'callFunction1()')

dFrame1.setURL(URLForPage1)

}

function createDFrameForPage2 {

dFrame2 = instantiateDFrame()

dFrame2.addButton('Action for Page 2', 'callFunction2()')

dFrame2.setURL(URLForPage2)

}

function instantiateDFrame() {

var x = new DFrame(parameters)

x.addButton('Close', 'thisDFrame.closeFrame()')

return x

}

Note: The Javascript code of the page inserted in DFrame will also be able to modify the appearance of DFrame. See the onLoadDframe function.

## Unique creation : global var

### Principle

In certain cases a DFrame is created in a single way and is identified like such. A global variable will then be used to maintain a handle on this dFrame.

### Example

In the preceding example each call of the functions createDFrameForPage1() or createDFrameForPage2() will start the creation of a new DFrame. To avoid this multiple creation a global variable is associated with each DFrame:

var dFrame1
var dFrame2
function createDFrameForPage1() {

if (!dFrame1) {

dFrame1 = instantiateDFrame()

dFrame1.addButton('Action for Page 1', 'callFunction1()')

}

dFrame1.setURL(URLForPage1)

}

function createDFrameForPage2 {

if (!dFrame2) {

dFrame2 = instantiateDFrame()

dFrame2.addButton('Action for Page 2', 'callFunction2()')

}

dFrame2.setURL(URLForPage2)

}

function instantiateDFrame() {

var x = new DFrame(parameters)

x.addButton('Close', 'this.closeFrame()')

return x

}

The first time that the functions createDFrameForPage1() or createDFrameForPage2() will be called the corresponding objects will be created. The following times only the methods dFrame.setURL(url) will be carried out and will cause only to bring to the first plan the concerned DFrames.

## Multiple creation: DFrameCollector

### Principle

In other cases it is on the contrary desired that each call of function involve the creation of a new DFrame. The following code corresponds to this situation:

function openDFrame(urlForDFrame) {

var dFrame = instantiateDFrame()

dFrame.setURL(urlForDFrame)

}

function instantiateDFrame() {

var x = new DFrame(parameters)

x.addButton('Close', 'thisDFrame.closeFrame()')

return x

}

Each time the function openDFrame is called, with a URL specified by the parameter urlForDFrame, a new DFrame will be created.

Each time the button 'Close' is used the DFrame is closed.

It can thus be interesting, in terms of memory and performances, to reuse the closed DFrames. The register and find methods of the DFrameCollector object, automatically instantiated during the loading of DFrameAPI, will allow it:

### Example :

function openDFrame(urlForDFrame) {

//define a key for this kind of dFrame

var key = 'keyForDFrameOfThisType'

//look for a DFrame with this key in the dFrameCollector

var dFrame = DFrameCollector.find(key)

//if not found: create a new dFrame

if ( !dFrame ) dFrame = instantiateDFrame(key)

dFrame.setURL(urlForDFrame)

}

function instantiateDFrame(key) {

var dFrame = new DFrame(parameters)

dFrame.addButton('Close', 'this.closeFrame()')

//register the dFrame in the DFrameCollector for reuse

DFrameCollector.register(dFrame, key)

return dFrame

}

When a DFrame recorded in the DFrameCollector by the DFrameCollector.register() method is closed it is stored in the DFrameCollector and becomes available for a research launched by the DFrameCollector.find() method .