Function Property
A Function Property returns the evaluation result of a StructrScript or JavaScript expression.
Details
A function property has a Read
and a Write
function. Both functions can contain a StructrScript or JavaScript expression(s).
The Read
statement is evaluated when the value of the function property is read, the Write
statement is evaluated when the value of the property should be written to the database.
This allows for function properties to perform arbitrary actions when a value is stored. (see the example for a simple write action)
Note: The normal surrounding ${}
for the Read
and Write
functions is unnecessary as it is inserted automatically. StructrScript is entered directly and JavaScript code has to be enclosed in {}
.
Example
In this example the User
type has a relationship with the Image
type which is called HAS_AVATAR
. The Read
and Write
functions serves as simple shortcuts to the name of the image.
Setup
-
A
User
can have an avatarImage
. -
The remote attribute is named
image
-
The Function property itself is named
avatarFilename
-
The read function serves as a simple shortcut to the
name
property of the Image -
The write function updates the remote object with the given value. The parameter is passed as
value
within the write function.
JSON Output
{
"result_count": 1,
"result": {
"id": "f02e59a47dc9492da3e6cb7fb6b3ac25",
"type": "User",
"image": {
"id": "ba2b00a7a77e40998d3fc1f2e5821e2e",
"name": "avatar.png"
},
"avatarFilename": "avatar.png"
}
}
Hint
To retrieve the parameter passed to the write function within a JavaScript
context, use Structr.get('value')
.