The apex.model namespace contains methods used to manage client side Oracle APEX 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
This example displays an alert message if any (non-local) models on the page have unsaved changes.
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
This example displays an alert message if any (non-local) models on the page have errors.
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 server's 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".
For record shape models can be false when pData is null to indicate
that there is no data. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pDataOverflow |
boolean |
<optional> |
If true there is more than the sever configured maximum allowed records for this model. In other words the database has more records in the result set than it is willing to return. Putting a limit on the amount of data that can be returned is typically done so that both client and server resources are not overwhelmed. Only applies for table shape models. |
Returns:
- Type
- model
Examples
This example creates a very simple local table shape model called "people" that stores name and age. The records are arrays and the model is given some initial data. The model is editable and the ID field is the record identity.
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 );
This example creates a table shape model that gets data from a REST service called from the
browser by using the callServer
option. The records are objects.
The example code only handles the fetch request.
function myRESTCallServer( requestData, options ) {
var deferred = apex.jQuery.Deferred(),
region = requestData.regions[0], // assume there is just one region in the request
offset = region.fetchData.firstRow - 1, // convert to zero-based if needed
count = region.fetchData.maxRows;
// make an ajax request using XMLHttpRequest or fetch API. Use offset and count to specify the page of
// data to fetch as part of the REST URL.
fetch( "some/REST/URL" ).then( function( response ) {
var responseData = response.json();
// suppose the REST service response is an array of records and the records do not have any nested
// structure that needs to be adjusted and the array is in a variable called responseData.
var response = {
regions: [ {
id: region.id,
ajaxIdentifier: region.ajaxIdentifier,
fetchedData: {
values: responseData,
firstRow: offset,
moreData: true
}
} ]
};
deferred.resolve( response )
} );
return deferred.promise();
}
var fields = ...;// define model fields according to what the REST service returns.
apex.model.create( "people", {
shape: "table",
recordIsArray: false,
fields: fields,
identityField: "ID", // this assumes the REST service returns a field with name "ID" that is the primary key
callServer: myRESTCallServer,
paginationType: "progressive"
}, null );
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
Destroy the model with model id MyModel.
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
Get access to a model with model id MyModel and release it when done.
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>
Fetches data for multiple models in a single Ajax request. In most cases there is no need to process the data of the promise because the models have already done so.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
pRequestData |
object |
<optional> |
An initial request object that will have fetch requests for the specified models added to it. |
pOptions |
object |
<optional> |
Options to pass on to apex.server.plugin API. |
pModelIds |
array | An array of model ids to fetch data for. | |
pCallServer |
function |
<optional> |
An optional function to call in place of apex.server.plugin. See the callServer option of apex.model.create for more information. |
Returns:
- Type
- null | promise
Example
The following example will refresh two interactive grid regions, r1 and r2, with a single ajax request.
// an array of the IG region views
let igViews = [
apex.region( "r1" ).call( "getCurrentView" ),
apex.region( "r2" ).call( "getCurrentView" )
];
// an array of the IG region view model ids to fetch
let modelIds = igViews.map( v => v.model.modelId() );
// for each of the views, clear the data without notifying the view
igViews.forEach( v => { v.model.clearData( false ) } );
apex.model.multipleFetch( null, {
loadingIndicatorPosition: "page"
}, modelIds ).done( () => {
// this assumes the IG regions only have grid views
igViews.forEach( v => { v.view$.grid( "refresh" ) } );
} );
// Compare with: apex.region( "r1" ).refresh(); apex.region( "r2" ).refresh();
// which results in 2 ajax requests. Simpler code but more network traffic.
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 apex.model.get or apex.model.create a call to apex.model.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
Get access to a model with model id MyModel and release it when done.
var myModel = apex.model.get("MyModel");
// ... do something with myModel
apex.model.release("MyModel"); // release it when done
(static) save(pRequestDataopt, pOptionsopt, pModelIdopt, pIncludeRelatedopt, pCallServeropt) → {null|promise}
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. |
pCallServer |
function |
<optional> |
An optional function to call in place of apex.server.plugin. See the callServer option of apex.model.create for more information. |
Returns:
- Type
- null | promise
Example
This example saves all the models on the page that have changes.
apex.model.save();
Parameters:
Name | Type | Description |
---|---|---|
n |
number | Number of unreferenced, unchanged detail instance models that will be kept. |