Anywhere API Reference - ModelService

Main page of the Anywhere API Reference

The ModelService class can be used to fetch data from server or JSON Store.
The ModelService.all method retrieves the data from the server, refreshes the data into the JSON Store then fetches the data from the JSON Store in a ModelDataSet. The ModelService class works asynchronously using Dojo promises.
Be aware that the result data is not loaded in app cache but can be added manually using Application.addResource method.

all(resourceName, [queryBase], [pageSize], [forceServerPreferred])

Returns a promise to the set.
The ModelService.all method retrieved data from the server, then refreshes the JSON Store with the results and then fetches the data from the JSON Store to be returned to the caller in a ModelDataSet. If the ModelService is unable to get the data from the server (say the device is disconnected) it will return the data from the JSON Store.

Parameters

  • resourceName
  • queryBase
  • pageSize
  • forceServerPreferred

Examples

var locationMetersPromise = ModelService.all('locationMeters', null, null, false);
locationMetersPromise.then(function(locationMeterSet)
{
  array.forEach(locationMeterSet.data, function(locationMeterRecord) {
    locationMeterRecord.set('localLastReading', null);
  
    locationMeterRecord.set('localLastReadingDate', null);
  });
  ModelService.save(locationMeterSet);
});

empty(resourceName, [queryBase], [parentModelData], [complexAttributeName])

Returns a promise to the empty set.

Parameters

  • resourceName - Name of the ModelDataSet to retrieve
  • queryBase (optional) -
  • parentModelData (optional) -
  • complexAttributeName (optional) -

Examples

ModelService.empty('workOrder').then(function()
{
  eventContext.ui.getViewFromId('WorkExecution.WorkItemsViewMineProgress');
  eventContext.ui.show('WorkExecution.WorkItemsViewMineProgress');
});

filtered(resourceName, queryBase, filter, ...)

Query a resource and returns a promise to the filtered set.

Parameters

  • resourceName
  • queryBase
  • filter – Array of attribute/values to be matched. See example.
  • pageSize (optional) – number of records to be returned in a page of the retuned data set
  • forceServerPreferred (optional)
  • isExactMatch (optional) –
  • oslcQueryArguments (optional)
  • forceLocalPreferred (optional)

Examples

ModelService.filtered('assetMeters',
                      null,
                      [{'assetnum': assetnum, 'siteid': siteid, 'active':true}],
                      1000,
                      false,
                      true).then(function(classStructure) {
  // do something
});

save(dataSet)

Commits any change in the data set and saves it on the local store. It will trigger a sync with the server if a connection is available.

Parameters

  • dataSet – The ModelDataSet object to be saved

Examples

ModelService.save(workOrderSet).then(function() {
  eventContext.ui.hideCurrentView();
}).otherwise(function(error) {
  Logger.log(JSON.stringify(error));
});

saveAll(dataSetArray)

Commits and change and saves all the data sets passed in the array.

Parameters

  • dataSetArray – Array of ModelDataSet objects

Examples

ModelService.saveAll([workOrderSet, woTimer.getOwner()]).then(function() {
  self.ui.hideCurrentView(PlatformConstants.CLEANUP);
}).otherwise(function(error) {
  self.application.showMessage(MessageService.createStaticMessage("errorwhilesavinglabor").getMessage());
});


3 comments:

  1. ModelService.save(data) always creates a new record in offline db. How to delete all records from db before saving fresh data.Please advise.

    I tried "deleteLocalAll" but its still having all data when i invoke ModelService.all(data). Please advise

    ReplyDelete
  2. Also after updating a record in offline db, we need to save it. Then it creates new record instead if updating existing record.Please help.

    ReplyDelete
    Replies
    1. This may be related to the ANYWHEREREFID attribute.
      Maximo Anywhere requires a new attribute to be added to the related/child object in Maximo such that it can identify and relate that new record in the response from Maximo with the one recently created locally (which does not have the Maximo assigned id yet). This attribute is called anywhererefid.
      Search for 'maximo anywhere anywhererefid' and you will find a PDF.

      Delete