Syntax
argument ::= declare_item [';' declare_item]* declare_item ::= ('required') variable_name (variable_type (expression)) variable_name ::= Name variable_type ::= Name
Description
The data-declare statement declares a list of variables and some settings about them.
Each declaration can define:
-
The required reserved word declares a required variable. It is optional. The default value is
false
. - The variable name. It is the only mandatory item.
- The variable type (optional). It declares the type of the variable.
- An expression working as default value (optional). It is evaluated only if the value of the variable is undefined.
Some example of types:
Type | Value example |
---|---|
number | NaN |
number | 5 |
array | [] |
string | 'any string' |
function | function(){} |
regexp | /a/ |
date | new Date() |
object | {} |
myconstructor | new MyConstructor() |
Any value matches undefined or null types.
A strict mode is set to true
inside the node with this attribute. Strict mode makes it easy to force to declare variables. If strict mode is true
and ZPT-JS finds a not declared variable an error occurs. Strict mode can be defined in two ways:
- Using a data-declare attribute. All the variables used in this node must be declared.
- Using zpt.context.setStrictMode( true ). All the variables used in the root node must be declared.
An error occurs if:
- A non declared variable is found.
- The value of a required variable is
undefined
. - The type of a variable does not match with the declared one.
If an error occurs ZPT-JS stop processing the nodes and show the error (using a javascript alert
by default). ZPT-JS uses the errorFunction defined in context. To customize it use the setErrorFunction in context.
Differences with ZPT
This statement does not exist in ZPT.
Examples
Declaring a number, a string, an array and a date:
data-declare="aNumber number; aText string; anArray array; aDateValue date"
Declaring a number, a string, an array and a date (all required):
data-declare="required aNumber number; required aText string; required anArray array; required aDateValue date"
Declaring a required variable (no type):
data-declare="required myVar"
Declaring some default values:
data-declare="aNumber number 99; aText string 'a default value'"