Statements: notifyobservers
The notifyobservers statement triggers an object or a property to notify observers attached to it.
Syntax
Form 1:
notifyobserversobject‑expression;
notifyobserversobject‑expression,id‑expression;
Form 2:
notifyobserversproperty‑reference‑expression;
notifyobserversproperty‑reference‑expression,id‑expression;
Form 3:
notifyobservers 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 observers
In its first version, the notifyobservers statement triggers the object specified in the object-expression to post signals to slot methods currently maintaining observer relations with this object. Object observer relations are established and dissolved by using the statements attachobserver and detachobserver. 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:
// Notify all observers attached to 'SomeObject' and having the particular 'id' notifyobservers SomeObject; // id = 0 assumed notifyobservers SomeObject, 1369; notifyobservers SomeObject, 0;
Please note, if there are more than one observer attached to the object, the order in which the affected slot methods are invoked is not predictable. If object-expression results in the value null, nothing occurs. If there are no matching observer attached to the object, nothing occurs.
Property observers
In its second version, the notifyobservers statement triggers the property specified in the property-reference-expression to post signals to slot methods currently maintaining observer relations with this property. Property observer relations are established and dissolved by using the statements attachobserver and detachobserver. For example:
// The slot method 'onUpdateData' is attached as observer to the property // 'Value' attachobserver onUpdateData, ^Value; [ ... ] // Triggers '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:
// Notify all observers attached to 'Value' and having the particular 'id' notifyobservers ^Value; // id = 0 assumed notifyobservers ^Value, 1369; notifyobservers ^Value, 0;
Please note, if there are more than one observer attached to the property, the order in which the affected slot methods are invoked is not predictable. If property-reference-expression results in the value null, nothing occurs. If there are no matching observer attached to the property, nothing occurs.
Global observers
When used in its third version, the notifyobservers statement posts signals to slot methods registered currently as global observers. Global observer relations are established and dissolved by using the statements attachobserver and detachobserver. With the unsigned integer id specified in the parameter id-expression you address the desired observers individually. 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;
Please note, if there are more than one matching observer, the order in which the affected slot methods are invoked is not predictable. If no observer match the id, nothing occurs.