Links

ContextService

ContextService TypeScript Class Reference

Overview

SERVICE This class is a "service" and it is automatically injected if declared as a member in the constructor of the consuming class. - See the constructor section below for details - See the Automatic Instance Injection page for details on the topic
The ContextService class provides methods to read and write pieces if information, which must be passed between views during the lifecycle of the current UI task. This class is used to exchange data between the views within the same task. When a new UI task is spawned, it also receives a brand new "context". The UI task "context" is a non-persistent memory space used to store temporary data and accessible from each view within the task.
Properties
Methods
Constructor
Platforms
App Types
No properties
  • get
  • set
  • clear
Don't instantiate directly instances of the ContextService class.
ContextService instances are automatically injected by Dynamics Mobile framework.
import {View, ContextService} from '$dms';
@View()
export class MyView {
constructor(
//we need to only declare the member
private ContextService: ContextService
){
}
async load(){
//we can now use the injected instance
let value = await this.ContextService.get('mysetting');
}
}
📳
Devices: All
Clouds: All
📳
Mobile
🌐
Backend

get(key: string): Promise<any>

Description
Parameters
Returns
Example
The get method returns the value stored under the key given as method argument.
  • key: string unique key of the item to retrieve from the task context. It must not contain only letters and digits and no spaces
Returns value of type any if the value stored under the given key or returns undefined if the key does not exists. The returned value may be casted to the appropriate type.
import { ContextService } from '@dms';
import { Customer } from '@dms-bo';
...
let customer = await this.ContextService.get('customer') as Customer;

set(key: string, value: string): Promise<void>

Description
Parameters
Returns
Example
Method set stores a value for the given key. The value can be later read via the get method.
  • key: string the value is stored under the given key. it must not contain only letters and digits and no spaces
  • value: any the value of the key. The value can be then read via the get method, later
void
import { ContextService } from '@dms';
import { Customer } from '@dms-bo';
...
let customer = new Customer();
customer.no = "1";
customer.name = "my customer";
await this.ContextService.set('customer',customer);

clear(): Promise<void>

Description
Parameters
Returns
Example
The clear method clears all key-value pairs from the current UI task's context. All subsequent calls to method get will return undefined. This method is not likely to be needed in most of the cases as the task context is cleared automatically on each UI task completion.
No parameters
void
import { ContextService } from '@dms';
...
await this.ContextService.clear();

How to use

import { View, ContextService } from '@dms';
import { Customer } from '@dms-bo';
@View()
export class MyView {
constructor(private ContextService: ContextService);
async load(): Promise<void> {
let customer = await this.ContextService.get('customer') as Customer;
console.log(`Customer name is ${customer.name}`);
}
}