The views (mobile screens) may pass information to each other. The concept is called Task Context.
The Dynamics Mobile Studio provides the concept for tasks steps and views- see the User Interface section for more information.
Each user activity is developed like a task, which contains a set of steps. The user navigates between the steps. Each step is associated with a view ( form ) displayed on the device’s screen.
Sometimes the developer will need to pass data between the steps/views and store data, which must be available during the execution of the task.
Dynamics Mobile provides special API for this purpose:
- The context object serves like a local (device memory) temporary storage for data
- The context object stores and extracts data by using unique keys – similar to a dictionary/hashtable
- The context object can only be used to pass data between steps/views within a single task
- The context is cleared before a task is launched by the user – can NOT be used to pass data between tasks
- Do not abuse the context object – avoid placing large objects like arrays of business objects and others because the context actually performs JSON serialization/deserialziation, which costs CPU cycles.
- The data within the context object will survive a web browser page refresh.
Let’s assume we have two views: View1 and View2 and that we have a task, where View1 is displayed first and then View2 is displayed. We want to pass some data from View1 to View2.
Let’s place some data into the context from View1:
// fetches a customer from the database
}); //placing the customer object into the context
The data can be placed in the context object by using the $ma.uiManager.context.set(key,data) method. The customer business object is placed inside the context and ready for consuming in this case.
Let’s get some data from the context from View2:
throw 'Strange, but customer is not in the context!';
$ma.uiManager.alert('The customer is there!');
The data can be retrieved by using the $ma.uiManager.context.get(key,defaultValue,success,failure) method. The get method will return the value supplied in the defaultValue parameter if no data exists under the given key. There is a second overload of the get method:
The developer can supply a default value, which will be returned if the context does not contain the requested key.
$ma.uiManager.alert('The customer object will always be there!');
The developer can also use $ma.uiManager.context.remove(key) method to remove data from the context.