There are two ways through which you can expose a property—simple values or functions. In other words, you can define a global variable and make it public or define a couple of get/put functions to gain more control over how the property is read or written. The DHTML Scriptlets provide only the function-based approach, while the Server Scriptlets provide both options.
Conceptually, a property is a variable so you should find it quite natural to implement it with a global buffer—freely accessible from both internal scripts and external callers. In most development tools, instead, you implement properties with a pair of functions which handle separately the reading and the writing. The variable-based approach is simpler to code but somewhat violates the encapsulation of the object. However, this is what you need to do to implement properties in terms of a variable.
<IMPLEMENTS ID=automation TYPE=Automation>
<PROPERTY name="Age" internalname="mAge" />
<PROPERTY name="Address" />
</IMPLEMENTS>
The tag <PROPERTY>
includes two attributes, name
and internalname
. The latter is optional. By name
you determine the externally visible name of the variable. Instead, internalname
denotes the name of the variable used internally to hold the value of the property. If you omit the internal name then the value is stored in a variable with the same name as the property.
In the example above, we defined a property called Age
but implemented it through a global variable called mAge
. We also defined an Address
property without an internal name. In this case, we absolutely need to declare and initialize a variable called Address
.
<SCRIPT language=JScript>
mAge = 32
Address = "30 Lincoln Road, Olton, Birmingham"
</SCRIPT>
The second way to declare properties is nearly identical to what we're used to doing with DHTML Scriptlets. That is—we must specify a function to return the value of the property and another one to allow the user to set a new content. This approach has two advantages:
In addition, we also gain in flexibility since we can now decide which access rights a property can have—read/write, write-only or read-only. In the first case, we specify a Get
and a Put
function. In the other two instances we omit the Get
and the Put
functions respectively. The Get
and the Put
functions of a property are equivalent to the internal functions we met in Chapter 3. In was here where we first described how to assign properties to a DHTML Scriptlet.
Here's how to re-declare the above two Age
and Address
properties in terms of Get
/Put
functions assuming both properties are read/write.
<IMPLEMENTS ID=automation TYPE=Automation>
<PROPERTY name="Age">
<GET internalname="getAge"/>
<PUT internalname="putAge"/>
</PROPERTY>
<PROPERTY name="Address">
<GET />
<PUT />
</PROPERTY>
</IMPLEMENTS>
There are a few things to notice in the above code. The first is the fact that the <PROPERTY>
tag is closed with the usual end-tag </PROPERTY>
. As explained earlier, this occurs because now the tag contains other tags, specifically <GET>
and <PUT>
. These tags, which can be equated to the leaves of the tree, render the <IMPLEMENTS>
tag and prevent the end-tag from exploiting the XML syntax facilities.
The <GET>
and <PUT>
tags allow us to specify the name of the internal functions that actually provide the expected behavior. To define a property in DHTML Scriptlets Name
we are used to writing:
this.get_Name = getInternalName;
this.put_Name = putInternalName;
where getInternalName
and putInternalName
are the names of the functions that will be called. By assigning a property get_Name
to the this
object we specify that our scriptlet has a read-enabled property called Name
. Alternatively, by assigning a property put_Name
we mean to enable external callers to write a property through the name Name
.
The <GET>
and <PUT>
tags are an equivalent and more general way to get the same result. By specifying such tags (or even only one of them)
<GET internalname="getAge"/>
<PUT internalname="putAge"/>
we enable the Server Scriptlet to add reading and writing capability to the property. In other words, the completely valid lines above are equivalent to the following for DHTML Scriptlets:
this.get_Age = getAge;
this.put_Age = putAge;
Again, internalname
is an optional attribute.
<PROPERTY name="Address">
<GET />
<PUT />
</PROPERTY>
If you avoid it, then the name of the working functions are assumed to be given by the exposed name of the property prefixed by get_
or put_
. In the case above, we need to implement functions named necessarily get_Address
and put_Address
.
Of course, the Get
function must not take any argument and return the current value of the property. By contrast, the Put
function needs a single parameter—the new value—and doesn't need to return anything.