Statements: attachobserver

The attachobserver statement registers a slot method to receive signals as soon as a notification is triggered for a specified property or object.

Syntax

Form 1:

attachobserverslot‑expression,object‑expression;

attachobserverslot‑expression,object‑expression,id‑expression;

Form 2:

attachobserverslot‑expression,property‑reference‑expression;

attachobserverslot‑expression,property‑reference‑expression,id‑expression;

Form 3:

attachobserverslot‑expression,null,id‑expression;

Abstract

Chora provides a unique language concept to broadcast notifications between GUI components. The connection between the sender of a notification and the affected recipients is established indirectly by associating both to a third object or property. Precisely, the interesting receiver 'attaches' itself as 'observer' to an object or property and the sender triggers the affected object or property to broadcast the notification to all registered observers. Both, the sender and the receiver don't know each other. As such, the functionality is ideal for implementing GUI application with the Model-View-Controller (MVC) software architecture pattern.

The established relations can be considered as 'weak references' and are ignored by the Garbage Collector. When the Garbage Collector reclaims an object, which is involved in an observer relation, the relation is dissolved automatically. In other words, one relation partner can't maintain alive another partner by the mere observer relation existing between them.

Object observer

In its first version, the attachobserver statement attaches a slot method resulting from the expression slot-expression as observer to the object specified in the object-expression. Once attached, the slot method will be invoked automatically when the object is notified by using the notifyobservers statement. For example:

// The slot method 'onUpdateData' is attached as observer to the object // 'SomeObject' attachobserver onUpdateData, SomeObject; [ ... ] // Triggers 'SomeObject' to notify all registered observers. This leads to // the invocation of the slot method 'onUpdateData'. notifyobservers SomeObject;

The optional unsigned integer parameter id-expression provides an additional subdivision to distinguish between several observers attached to one and the same object. Using the id-expression allows you to address the desired observers individually. If the expression is omitted, the statement assumes automatically the value 0 (zero) for the id. For example:

// Attached some slot methods as observer to the object // 'SomeObject'. attachobserver onUpdateData1, SomeObject; // id = 0 assumed attachobserver onUpdateData2, SomeObject, 1369; attachobserver onUpdateData3, SomeObject, 0; [ ... ] // Triggers 'SomeObject' to notify all registered observers with // the id = 0. This leads to the invocation of the slot methods // 'onUpdateData1' and 'onUpdateData3'. notifyobservers SomeObject; // Triggers 'SomeObject' to notify all registered observers with // the id = 1369. This leads to the invocation of the slot method // 'onUpdateData2' only. notifyobservers SomeObject, 1369;

If one of the both parameters slot-expression or object-expression results in the value null, the operation is simply ignored and no observer is attached. If the specified slot method is already attached as observer to the given object, nothing occurs, the slot method is not attached twice. For example:

var object someObject = null; // The local variable 'someObject' contains null. The operation is ignored. attachobserver onUpdateData, someObject; // The slot method 'onUpdateData' is attached as observer to the object // 'SomeObject' attachobserver onUpdateData, SomeObject; [ ... ] // The slot method is already attached. The operation is ignored. attachobserver onUpdateData, SomeObject;

With the detachobserver statement once established observer relation can be dissolved again.

Property observer

In its second version, the attachobserver statement attaches a slot method resulting from the expression slot-expression as observer to the property specified in the property-reference-expression. Once attached, the slot method will be invoked automatically when the corresponding property is notified by using the notifyobservers statement. For example:

// The slot method 'onUpdateData' is attached as observer to the property // 'Value' attachobserver onUpdateData, ^Value; [ ... ] // Triggers the property 'Value' to notify all registered observers. This leads // to the invocation of the slot method 'onUpdateData'. notifyobservers ^Value;

The optional unsigned integer parameter id-expression provides an additional subdivision to distinguish between several observers attached to one and the same property. Using the id-expression allows you to address the desired observers individually. If the expression is omitted, the statement assumes automatically the value 0 (zero) for the id. For example:

// Attached some slot methods as observer to the property // 'Value'. attachobserver onUpdateData1, ^Value; // id = 0 assumed attachobserver onUpdateData2, ^Value, 1369; attachobserver onUpdateData3, ^Value, 0; [ ... ] // Triggers 'Value' to notify all registered observers with // the id = 0. This leads to the invocation of the slot methods // 'onUpdateData1' and 'onUpdateData3'. notifyobservers ^Value; // Triggers 'Value' to notify all registered observers with // the id = 1369. This leads to the invocation of the slot method // 'onUpdateData2' only. notifyobservers ^Value, 1369;

If one of the both parameters slot-expression or property-reference-expression results in the value null, the operation is simply ignored and no observer is attached. If the specified slot method is already attached as observer to the given property, nothing occurs, the slot method is not attached twice. For example:

var object someObject = null; // The local variable 'someObject' contains null. The operation is ignored. attachobserver onUpdateData, someObject; // The slot method 'onUpdateData' is attached as observer to the property // 'Value' attachobserver onUpdateData, ^Value; [ ... ] // The slot method is already attached. The operation is ignored. attachobserver onUpdateData, ^Value;

With the detachobserver statement once established observer relation can be dissolved again.

Global observer

When used in its third version, the attachobserver statement attaches a slot method resulting from the expression slot-expression as a kind of globally valid observer for the null object. With the unsigned integer id specified in the parameter id-expression you address the desired observers individually. Once attached, the slot method will be invoked automatically when the corresponding version of the notifyobservers statement is used. For example:

// The slot method 'onUpdateData' is attached as a global observer attachobserver onUpdateData, null, 1251; [ ... ] // Notify all globally registered observers with the id 1251. This leads to // the invocation of the slot method 'onUpdateData'. notifyobservers null, 1251;

If the parameter slot-expression results in the value null, the operation is simply ignored and no observer is attached. If the specified slot method is already attached as a global observer with the specified id, the slot method is not attached twice. For example:

var slot someSlotMethod = null; // The local variable 'someSlotMethod' contains null. The operation is ignored. attachobserver someSlotMethod, null, 1251; // The slot method 'onUpdateData' is attached as a global observer attachobserver onUpdateData, null, 1251; [ ... ] // The slot method is already attached. The operation is ignored. attachobserver onUpdateData, null, 1251;

With the detachobserver statement once established observer relation can be dissolved again.