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());
});
Scroll to top