Function Property

Search
Feedback

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
  1. A User can have an avatar Image.

    Schema setup

  2. The remote attribute is named image

    Remote Attribute

  3. The Function property itself is named avatarFilename

    Function Property

  4. The read function serves as a simple shortcut to the name property of the Image

    Read Function

  5. The write function updates the remote object with the given value. The parameter is passed as value within the write function.

    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').

Graph-Browser

About this article
Last change 2017-05-04
Topics Structr 2.0