Receiving Updates from the Server

by Tanja Buttler

Getting messages about new attachments and deleted attachments

When developing a collaborative application we are not only interested in sending changes a user has made to the server, we also want to be informed when other users have made changes. We use an instance of ActionCenterListener to register objects as listeners to these updates. Information about new attachments and deleted attachments are broadcasted on a channel publishing updates about relationships to the superior contribution. The following listing shows how to register an observer instance. This instance has two methods, attachmentAddedMethod and attachmentRemovedMethod, which are called when a new attachment is added to the superior contribution or removed from the superior contribution.

        
	this.addAttachmentSetObserver = function(observer, attachmentAddedMethod,
			attachmentRemovedMethod, loadMonitor) {
		var relType = populationRules.getAllChildTypeList()[0].relationship;
		var attachmentType = populationRules.getAllChildTypeList()[0].type;
		this.actionCenterListener.getSubordinates(this.parentContribution
				.getId(), relType, attachmentType, observer,
				attachmentAddedMethod, false, observer,
				attachmentRemovedMethod, loadMonitor, false);
	};

	
        

We have handed over the relationship type, the type of the attachment, and the ID of the superior contribution as parameters. These parameters specify the channel to which the observers registered.

Getting messages about changed attachments

Listening to changes made to an attachment is handled similarly. We use the method getContributionUpdates of the ActionCenterListener, and hand in the ID of the attachment to specify the channel to which we want to listen (see the following listing).

        
	this.addAttachmentObserver = function(contributionId, observer,
			attachmentChangedMethod) {
		this.actionCenterListener.getContributionUpdates(contributionId,
				observer, attachmentChangedMethod);
	};
	
        

Destroying the attachment service

When working with an ActionCenterListener we have to explicitly destroy it when the component instance is destroyed. Therefore our attachment service needs a method to clean up itself:

        
	this.destroy = function() {
		populationRules = null;
		if (this.actionCenterListener) {
			this.actionCenterListener.destroy();
			this.actionCenterListener = null;
		}
		this.parentContribution = null;
	};