Action Service Methodology - tsgrp/HPI GitHub Wiki

The Action Service Methodology is the concept that every action in HPI should have its own Action.Service that other files in HPI can call to execute that action. This provides a single place for the code to reside, allowing for changing of parameters to be done easily and less duplicate code to be written.

Structure

An action service should contain an execute function that will accept a javascript object that can contain the following structure:

{
  parameters : {}, //all the parameters needed to execute the action
  successFunction: function(){}, //An optional custom on success function
  errorFunction: function(){}, //An optional custom on error function
  global: true, //whether the execution should run with global false or true
  async: false, //whether the execution should be synchronous or not
} 

The action service should also determine what parameters can be optionally passed in or not. For instance, if the parameter isADocument is true 90% of the time; an action service may determine to default isADocument to true unless told otherwise.

Example

Here is FolderNotes.Service as an example.

FolderNotes.Service = {
	getParameters: function(options){
		var currentDate = DateFormat.format.date(new Date(),"E, d MM yyyy hh-mm-ss a");
		return {
			note_id: options.note_id || "",
			parentID: options.parentID,
			objectId: options.objectId || options.parentID,
			note_content: options.note_content,
			note_rel_type: options.note_rel_type,
			note_object_type: options.note_object_type,
			property_map:{
				note_attachment: options.property_map.note_attachment,
				note_type: options.property_map.note_type || window.localize("modules.actions.exportFoler.folderNote"),
				objectName: options.property_map.objectName || "Folder Note - " + currentDate
			}
		};
	},
	execute: function(options){
		var defaultSuccess = function(){
			app.log.debug("Folder notes executed successfully");
		};
		var defaultError =  function(){
			app.log.debug("Folder notes executed with an error");
		};
		options.parameters = this.getParameters(options.parameters);			
		var action = new Action.Model({
			name:"folderNotes",
			parameters: options.parameters
		});
		action.execute({
			success: options.successFunction || defaultSuccess,
			error: options.errorFunction || defaultError,
			global: options.global || false,
			async: options.async || true
		});
	}
};	

And here is a downloaddocument.js calling the FolderNotes.Service:

FolderNotes.Service.execute({
	parameters: {
		parentID: parentId,
		note_content: noteContent,
		note_rel_type: self.config.get("folderNoteRelationship"),
		note_object_type: self.config.get("folderNoteObjectType"),
		property_map:{
			note_attachment: attachment,
			note_type: window.localize("modules.actions.downloadDocument.documentDownloaded")
		}
	}
});