Executing multiple actions at once
MultipleActionController allows to execute a set of actions within a single call. The results of all actions will be returned at once. Besides the performance gain you'll get by doing less script calls, this is especially useful, if you want to make sure that a set of actions is executed in a defined order. The orignal proposal for this Controller can be found
here. The implementation follows the initial proposal except for different naming of the special values.
To summarize, MultipleActionController expects a
data parameter that is an associative array with unique/sortable keys and values that describe an action to perform. It returns a data parameter that is an associative array with the same keys and values that describe the resonse of each action. The
special variable last_created_oid might be used as a placeholder for the last inserted object id. The variable needs a parameter, that defines the type of the inserted object. The notation is
{last_created_oid:type}, e.g.
{last_created_oid:ChiGoal}.
Example
Let's look at an example. Say you want to insert a
ChiGoal instance and display it afterwards. This can be done by combining the
new action with the
display action. In this case the
data parameter for the controller would look like:
data: {
action1: {
usr_action: "new",
type: "ChiGoal"
}
action2: {
usr_action: "display",
oid: {last_created_oid:ChiGoal},
omitMetaData: true
}
}
As the
result we would expect something like (actually the response has additional values due to technical reasons):
data: {
action1: {
oid: "ChiGoal:123",
success: "1"
}
action2: {
oid: "ChiGoal:123",
node: {
"0": {
modified: "2001-01-01 01:01",
creator: "admin"
...
}
...
},
success: "1"
}
}
Maybe your client is communicating with the wCMF backend using
JSON. So you might be using
ExtJs to make the call. The function for making the call could look like:
function doAction() {
// define the data parameter and encode it to JSON
data = Ext.encode({
action1:{usr_action: "new", newtype: "ChiGoal"},
action2:{usr_action: "display", oid: "{last_created_oid:ChiGoal}", omitMetaData: true}
});
// do the request
Ext.Ajax.request({
url: APPLICATION_URL,
method: "POST",
params: {
sid: sessionId,
controller: 'TerminateController',
usr_action: 'multipleAction',
response_format: 'JSON',
request_format: 'JSON',
data: data
},
callback: function (options, success, response) {
....
}
})
}
Ther are two things to note:
- The controller parameter is set to TerminateController (not ExitController as usually in single action calls). This makes sure that each call is terminated correctly and does not stop script execution.
- The request_format parameter is set to JSON. Otherwise wCMF would assume that the parameters are given in the default key/value pair format.