Skip to main content

Creating the Model Class: Methods

Next we implement several of the %ZEN.DataModel.ObjectDataModelOpens in a new tab callback methods. The controller executes these server-side methods in response to form (view) events.

Add the following four methods to ZenTutorial.ContactModel:

  1. 
    Method %OnOpenSource(pID As %String) As %RegisteredObject
    {
      Quit ##class(ZenTutorial.Contact).%OpenId(pID)
    }
    

    The dataController invokes this method to open an existing database object.

  2. 
    Method %OnSaveSource(pSource As ZenTutorial.Contact) As %Status
    {
       Set tSC = pSource.%Save()
       Set ..%id = pSource.%Id()
       Quit tSC
    }
    

    The dataController invokes this method when saving a database object.

  3. 
    Method %OnNewSource(Output pSC As %Status = {$$$OK}) As %RegisteredObject
    {
      Quit ##class(ZenTutorial.Contact).%New()
    }
    

    The dataController invokes this method to create a new database object.

  4. 
    ClassMethod %OnDeleteSource(pID As %String) As %Status
    {
      Quit ##class(ZenTutorial.Contact).%DeleteId(pID)
    }
    

    The dataController invokes this method to delete a database object.

Next, add the following method to ZenTutorial.ContactModel. This method loads data from a database object (ZenTutorial.Contact) into a model object (ZenTutorial.ContactModel).


Method %OnLoadModel(pSource As ZenTutorial.Contact) As %Status
{
    Set ..Name = pSource.Name
    Set ..ContactType = pSource.ContactType
    Set ..Street = pSource.Address1.Street
    Set ..City = pSource.Address1.City
    Set ..State = pSource.Address1.State
    Set ..Zip = pSource.Address1.Zip  
    Quit $$$OK
}

Finally, add the following method to ZenTutorial.ContactModel. This method writes the data from a model object (ZenTutorial.ContactModel) into a database object (ZenTutorial.Contact) and saves the database object.


Method %OnStoreModel(pSource As ZenTutorial.Contact) As %Status
{
    Set pSource.Name = ..Name
    Set pSource.ContactType = ..ContactType
    Set pSource.Address1.Street = ..Street
    Set pSource.Address1.City = ..City
    Set pSource.Address1.State = ..State
    Set pSource.Address1.Zip = ..Zip
    Quit pSource.%Save()
}
FeedbackOpens in a new tab