You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

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

Search results for "Function Property"

Passive Indexing

Passive indexing is the term for reading a dynamic value from a property (e.g. Function Property or Boolean Property) at application level, and writing it into the database at the end of each transaction, so the value is visible to Cypher. This is important for BooleanProperty, because its getProperty() method returns false instead of null even if there is no actual value in the database. Hence a Cypher query for this property with the value false would not return any results. Structr resolves this by reading all passively indexed properties of an entity, and writing them into the database at the end of a transaction.

Sorting

By default, the results of a GET request are *unorderd, and the desired sort order can be controlled with the (optional) parameters sort and order. You can sort the result by one or more indexed property value (including Function Property results), in ascending (_order=asc) or descending order (_order=desc), as shown in the following examples. The default sort order is ascending order. String sort order depends on the case of the value, i.e. upper case Z comes before lower case a.