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.
1
import {View, ContextService} from '$dms';
2
3
@View()
4
export class MyView {
5
6
constructor(
7
//we need to only declare the member
8
private ContextService: ContextService
9
){
10
11
}
12
13
async load(){
14
//we can now use the injected instance
15
let value = await this.ContextService.get('mysetting');
16
17
}
18
19
}
Copied!
📳
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.
1
import { ContextService } from '@dms';
2
import { Customer } from '@dms-bo';
3
...
4
5
let customer = await this.ContextService.get('customer') as Customer;
Copied!

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
1
import { ContextService } from '@dms';
2
import { Customer } from '@dms-bo';
3
...
4
5
let customer = new Customer();
6
customer.no = "1";
7
customer.name = "my customer";
8
await this.ContextService.set('customer',customer);
Copied!

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
1
import { ContextService } from '@dms';
2
...
3
await this.ContextService.clear();
Copied!

How to use

1
import { View, ContextService } from '@dms';
2
import { Customer } from '@dms-bo';
3
4
5
@View()
6
export class MyView {
7
8
constructor(private ContextService: ContextService);
9
10
async load(): Promise<void> {
11
let customer = await this.ContextService.get('customer') as Customer;
12
console.log(`Customer name is ${customer.name}`);
13
}
14
15
}
Copied!
Last modified 1yr ago