Receiving Updates from the Server - Part 2

by Tanja Buttler

In the previous section of this tutorial we have shown you how to get notifications about changes made to a contribution. In this section we are going to develop an observer that actually processes the incoming messages from the server.

The following listing shows the constructor of this AttachmentListObserver.

	var AttachmentListObserver = function(cache2, attachmentService) {

		this.cache = cache2;
		this.service = attachmentService;
		//decodes the msg (about an update to a contribution) and returns a new ActionCenterContribution 
		this.processMsg = function(msg) {
			return new ActionCenterContribution(;

The AttachmentListObserver has a method processMsg for processing the incoming messages from the server. A message is an object with a property data, the property data in turn has a property contribution. This property contains information about the changes. We use it to create an ActionCenterContribution supporting us in reading the content of contribution.

The client uses a cache for keeping a local copy of a list of attachments. When processing incoming updates the observer updates that cache. The following listing shows the interface of this local cache.
var AttachmentCache = function(component, helper) {//this object is a stub

	this.doAddAttachment = function(attachmentId, attachmentLabel, file) {};

	this.doRemoveAttachment = function(attachmentId) {};

	this.doUpdateAttachment = function(attachmentId, newAttachmentLabel) {};

Listening to Adding Contributions

In the following we look at the method that is called whenever a new attachment is added to the superior contribution under observation (see also the previous section on the listener mechanisms).

	this.added = function(msg) {
		var contribution = this.processMsg(msg);
		//Register for updates about the new attachment
		this.service.addAttachmentObserver(contribution.getId(), this,
		//Add the new attachment to the local cache
		if (this.cache) {
			this.cache.doAddAttachment(contribution.getId(), contribution
					.getPropertyValue('name'), contribution


First, we use the attachment service to register the observer for updates about the new attachment. Then we add this attachment to the local cache. Here we use the method getId of the ActionCenterContribution to access the ID of the attachment, and we use the method getPropertyValue to get the values associated with the keys defined in the attachment meta-model (see section XXX).

Listening to Removing Contributions

Processing removed attachments works similarly to processing added attachments. The ActionCenter API does not allow us to remove observers listening for changes to a specific attachment. Therefore we only have to remove the attachment from our local cache.

	this.removed = function(msg) {
		var contribution = this.processMsg(msg);
		// the underlying ActionCenter API does not support removing this type of listener
		//	this.service.removeAttachmentObserver(contribution.getId());
		//Remove the attachment from the local cache
		if (this.cache) {


Listening to Changes of a Contribution

Finally, our observer listens to changes made to an existing attachment. Here we are particularly interested in changes of the property name. Whenever such a change is made we update the local copy of the contribution:

	this.changed = function(msg) {
		var contribution = this.processMsg(msg);
		//update the name property of the attachment
		if (this.cache) {
					contribution.getPropertyValue('name'), null);