The apex.model namespace contains methods used to manage client side Application Express data models. These models store data for display by UI components. They correspond to the view-model in the Model-View-ViewModel (MVVM) pattern. See model for details.
This namespace contains functions to manage the lifecycle of a model:
- Use apex.model.create to create a model.
- Use apex.model.list to list all the existing models.
- Use apex.model.get to return an existing model.
- Use apex.model.release to release a model once you are done with it.
Models are reference counted so for every call to get
or
create
you must call release
. Failure to do so can
result in unused models taking up memory. Typically the APEX region associated with the model will manage
its life cycle.
Models typically act as an intermediary between data persisted on the server and one or more views on the client.
The regionId
option associates the model with an APEX region for the purpose of
fetching and saving data. Models can be created without a regionId
. These are
known as local models and they cannot fetch data from or save data to the server.
There are also methods such as apex.model.save, apex.model.anyChanges, and apex.model.anyErrors that operate on multiple models.
Models can be arranged in a master detail configuration. This is done by providing the
parentModel
and parentRecordId
options when creating the detail models. A single master model can have multiple kinds of detail models. For example
projects can have tasks and members as details. Each kind of detail model has one or more model instances; each related
to a record in the master model. Detail instance models share the same name and field configuration but each
has a distinct instance id and different data. A model is uniquely identified by a model.ModelId, which in the case
of a detail model contains the detail name and instance id. Detail models are cached so that data doesn't have to be
fetched from the server unnecessarily. The view layer typically shows a view of the detail instance model that is
associated with the current record of the master view. As the current record of the master changes the view layer
changes the detail model instance the detail view is showing. The view layer will get a cached instance model if
there is one and if not will create the instance model. The maximum number of detail instances to cache is controlled
with the apex.model.getMaxCachedModels and apex.model.setMaxCachedModels functions. It is the least
recently used model that is kicked out of the cache. Models that have changes are not destroyed unless
apex.model.destroy is called.
A detail model can be a master to its own set of sub-detail models. This relationship can be nested to any depth.
- Since:
- 5.1
Low level function to add changes for any of the specified models to a request. Changes are added to the provided request data. This doesn't actually send the request to the server. In most cases apex.model.save should be used rather than this function.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
pRequestData |
object | An initial request object that will have all changes for the specified models added to it. | |
pModelId |
model.ModelId |
<optional> |
Model identifier as given in call to apex.model.create or just a model name. See apex.model.list for how this parameter is used to select which models to operate on. |
pIncludeRelated |
boolean |
<optional> |
If true then any dependents of any selected models are included if they have changes. |
Returns:
- Type
- function
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
pIncludeLocal |
boolean |
<optional> |
If true models that don't have a regionId
will be included in the check for changes. |
pModelId |
model.ModelId |
<optional> |
Model identifier as given in call to apex.model.create or just a model name. See apex.model.list for how this parameter is used to select which models to operate on. |
pIncludeRelated |
boolean |
<optional> |
If true then any dependents of any selected models are included in check |
Returns:
- Type
- boolean
Example
if ( apex.model.anyChanges() ) {
apex.message.alert("Save Changes");
}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
pIncludeLocal |
boolean |
<optional> |
If true models that don't have a regionId
will be included in check for errors. |
pModelId |
model.ModelId |
<optional> |
Model identifier as given in call to apex.model.create or just a model name. See apex.model.list for how this parameter is used to select which models to operate on. |
pIncludeRelated |
boolean |
<optional> |
If true then any dependents of any selected models are included in check. |
Returns:
- Type
- boolean
Example
if ( apex.model.anyErrors() ) {
apex.message.alert("Fix Errors");
}
(static) create(pModelId, pOptions, pDataopt, pTotalopt, pMoreDataopt, pDataOverflowopt) → {model}
Create a model with the given identity, options and optionally initial data. When you are done with the model you must call apex.model.release. Or if you are sure no one else is using it you can call apex.model.destroy.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
pModelId |
model.ModelId | Model identifier. Must be unique for the page. Creating a model with an identifier that already exists will overwrite the existing model. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pOptions |
object | Model options. All properties are optional unless specified otherwise.
Properties
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pData |
array | object |
<optional> |
Initial data to add to the model. For table shape data it is an array of model.Record. For tree shape models it is a model.Node for the root. For record shape data it is a single model.Record. If null or not given there is no initial data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pTotal |
number |
<optional> |
Total number of records in the servers collection. Only applies for table shape models. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pMoreData |
boolean |
<optional> |
If true there is more data available on the server for this model. If false
pData contains all the data. If omitted or null determine if there is more
data based on pData and pTotal .
If pTotal is not given assume there is more data on server.
Only applies for table shape models and only if paginationType is not "none". |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pDataOverflow |
boolean |
<optional> |
If true there is more than the maximum allowed records for this model. Only applies for table shape models. |
Returns:
- Type
- model
Example
var fields = {
ID: {
index: 0
},
NAME: {
index: 1
},
AGE: {
index: 2
}
},
data = [
["00010", "Mark", "32"],
["00091", "Mary", "27"],
...
];
apex.model.create("people", {
shape: "table",
recordIsArray: true,
fields: fields,
identityField: "ID",
editable: true,
paginationType: "none"
}, data, data.length );
Destroy and remove a model by its identifier. This bypasses reference counting and caching. This method should not be used unless you are sure that no one else is using the model.
If pModelId
is a string model name and there are one or more instances
they will all be destroyed.
Parameters:
Name | Type | Description |
---|---|---|
pModelId |
model.ModelId | Model identifier as given in call to apex.model.create or just a model name. |
Example
apex.model.destroy("MyModel");
(static) get(pModelId) → {model}
Parameters:
Name | Type | Description |
---|---|---|
pModelId |
model.ModelId | Model identifier as given in call to apex.model.create. |
Returns:
- Type
- model
Example
var myModel = apex.model.get("MyModel");
// ... do something with myModel
apex.model.release("MyModel"); // release it when done
Returns:
- Type
- number
(static) list(pIncludeLocalopt, pModelIdopt, pIncludeRelatedopt) → {Array.<model.ModelId>}
Returns an array of all the currently defined model identifiers in no particular order.
If pModelId
is null or not provided all models are listed.
If pModelId
contains just a model name then just that model if any and all
instances with the same model name if any are returned.
If pModelId
contains a model and an instance then just that model instance is included.
Specifying pModelId
is most useful when pIncludeRelated
is true.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
pIncludeLocal |
boolean |
<optional> |
If true models that don't have a regionId will be included. |
pModelId |
model.ModelId |
<optional> |
Model identifier as given in call to apex.model.create or just a model name. |
pIncludeRelated |
boolean |
<optional> |
If true then any dependents of any listed models are included. |
Returns:
- Type
- Array.<model.ModelId>
Release a model if it is not being used but may be used again in the future. This allows the model to be destroyed if needed to conserve memory.
Models are reference counted. For every call to get or create a call to release with the same model id is required. When the reference count is zero the model is destroyed unless it is changed or if it has a parent model, in which case it is cached.
Parameters:
Name | Type | Description |
---|---|---|
pModelId |
model.ModelId | Model identifier as given in call to apex.model.create. |
Example
var myModel = apex.model.get("MyModel");
// ... do something with myModel
apex.model.release("MyModel"); // release it when done
Save any of the specified models that have changes. This consolidates all the model data to save into a single request.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
pRequestData |
object |
<optional> |
An initial request object that will have all changes for the specified models added to it. |
pOptions |
object |
<optional> |
Options to pass on to apex.server.plugin API. |
pModelId |
model.ModelId |
<optional> |
Model identifier as given in call to apex.model.create or just a model name. |
pIncludeRelated |
boolean |
<optional> |
If true then any dependents of any selected models are included in check. |
Returns:
- Type
- promise
Example
apex.model.save();
Parameters:
Name | Type | Description |
---|---|---|
n |
number | Number of unreferenced, unchanged detail instance models that will be kept. |