Submitting Data to the Server
This chapter describes how to submit data from a Zen Mojo page to the server. It discusses the following:
For an overview, see “Submitting Data,” in the first chapter.
Submitting Data to the Server
To enable your page to send data to the server, do the following:
-
In the template class, define a method that sends a submit object to the server. In this method:
-
Obtain any needed values from the page. To get the value of a control, use getControlValue(). Also see the chapter “Interacting with Layout Objects.”
-
Create a submit object, which is a JSON object that carries the data to be submitted. This object can have any properties you need.
-
Invoke the page method submitData(), using the submit object as an argument.
This method has the following signature:
ClientMethod submitData(key, data, notify) [ Language = javascript ]
Where:
-
key — The key to use when sending data to the server
-
data — A JSON object that carries the data to be sent to the server. You can omit this argument if there is no data to submit; that is, you do not need to include an empty object.
-
notify — Either null or a function. If this argument is a function, the page sends the data asynchronously to the server (rather than synchronously). Then , when the data has been sent, the page executes the function named by this argument.
This method invokes the %OnSubmitData() method of the template class. See the next bullet item.
-
-
If desired, use data contained in the response object returned by submitData().
-
-
In the template class, define the %OnSubmitData() method. This method has the following signature:
ClassMethod %OnSubmitData(pKey As %String, pID As %String, pSubmitObject As %RegisteredObject, ByRef pResponseObject As %RegisteredObject) As %Status
This method returns pResponseObject by reference.
-
In the page class, within a suitable event handler or callback, invoke the method that you created in the first bullet item.
Example
The following shows a simple example for a method that sends a submit object to the server:
ClientMethod myMainViewSelect(key, value) [ Language = javascript ]
{
var mainView = zen('mainView');
switch(key) {
case 'submitEdits':
var controlvalue1=mainView.getControlValue('enterName');
var controlvalue2=mainView.getControlValue('enterCity');
var submitobject={name:controlvalue1,city:controlvalue2};
var response = zenPage.submitData('submitEdits',submitobject);
if (response && response.message) {
zenPage.showMessage(response.message);
}
mainView.updateLayout('fade');
break;
}
The template class defines the %OnSubmitData() method as follows:
ClassMethod %OnSubmitData(pKey As %String, pID As %String, pSubmitObject As %RegisteredObject,
ByRef pResponseObject As %RegisteredObject) As %Status
{
Set tSC = $$$OK
Try {
if pKey="submitEdits"{
set id=1
set tPerson = ##class(Sample.Person).%OpenId(id)
set tPerson.Name = pSubmitObject.name
set tPerson.Home.City = pSubmitObject.city
Set pResponseObject = ##class(%ZEN.proxyObject).%New()
Set tSC = tPerson.%Save()
If $$$ISERR(tSC) {
Set pResponseObject.message = "Your changes were not successfully saved"
Quit}
Set pResponseObject.message = "Your changes have been successfully saved"
}
}
Catch(ex) {
Set tSC = ex.AsStatus()
}
Quit tSC
}