What is Context and how can I use it?

15
When creating forms in the Modeler, some widgets have properties related to something called "Context". What is this "Context" and how can I use it in my project?
asked
3 answers
20

Contexts are a mechanism of the client to keep track of where the user is and has been in an application in relation to the data model. If the context parameters of a widget have not been changed from the defaults in the Modeler, the client will behave as if the user of the application is making a walk through the Metamodel.

For example, say we have a model with Customer, Order and OrderItem objects, where the relationship is such that a Customer has many Orders (Customer - 1:n - Order) and an Order has many OrderItems (Order - 1:n - OrderItems).

A trivial application to add or edit new Orders for existing customers would start with a form containing a DataGrid listing Customers. Selecting a Customer in this DataGrid is linked to another form containing a DataGrid listing Orders. If the context parameters are not changed from the defaults, this Order DataGrid will be started in the context of the Customer that was previously selected.

This means that the Order DataGrid will be filled only with Orders that belong to the selected Customer. (The client will generate an XPath query that matches Orders for the selected Customer : //Order[Customer_Association='ID_12345'] ).

If a new Order is created by this Order DataGrid, the new Order object will automatically have its association field set to the identifier of the Customer object in whose context it was created. In other words, the new Order object is automatically attached to the Customer whose Orders are being worked on. When the Order is saved, the Order DataGrid for this Customer receives an update signal and, well, updates accordingly.

Now if you have a form for creating or editing an Order, you will usually have fields to edit or display data (generally the value of attribute fields) relating to the Order object in question. You might also want to show the OrderItems belonging to this Order in form. You can just put a DataGrid for OrderItems in this form and this DataGrid will run in the context of the Order you are working on.

The OrderItem DataGrid in the Order form will only show OrderItems associated with this specific Order using the same mechanism that was used to constrain (via generated XPath) Orders belonging to a particular Customer. New Orders will obviously have no OrderItems associated with them and the OrderItem DataGrid will be empty in this view of the form.

A new OrderLine created from this Order DataGrid, will automatically be set to its context, which means that the association field to the parent Order will automatically be set to this reference. This works exactly the same way that creating an Order for a Customer did. The names of the entities in the model are different, but their relationships and behavior are identical.

To sum it up, contexts are a mechanism that facilitates the 'do the obvious thing' logic for application developers.

E.g. "Show Orders only for the Customer I previously selected" or "Only show OrderLines for the Order this form is working on" or "Because I'm creating a new Order inside this form that deals with a Customer - make this Order belong to that Customer without asking me".

You usually want this functionality. If you don't want it at a certain point in your application, you have the option of clearing the context a widget (or new piece of content) will execute in (the reset option).

*Edit* : This next paragraph applies to Mendix 2.4 and older. Most of the context manipulation flags have been obsoleted from 2.5 onwards and will appear under the 'deprecated' header in the relevant Modeler properties list.

The Modeler allows you to manipulate context flags on a variety of widgets. The basics are 'keep', 'user' and 'reset'.

  • 'keep' is just follow the default behavior (you're telling the client to do automagic stuff)
  • 'user' adds the current User object to the context (all queries will be constrained to this User if an association to whatever we entity we are forming the XPath query exists)
  • 'reset' destroys the Context - it makes a clean slate, all subsequent queries will be unconstrained

The default contexting mechanism is both powerful and useful. If you are just starting to build your own application for the first time, I suggest you just ignore the widget flags in the Modeler that deal with contexts. The default behavior will work for you. ('Walking the model').

answered
8

Context in general is used to constrain the data that is shown in the user interface as you navigate through an application. I would advise you to go to Context introduction first and read the introduction. This introduction explains what implementations of context exist and shows some examples of these implementations

answered
6

Context is too complex to explain here in a simple answer, Check the wiki for more information: https://world.mendix.com/display/refguide/Context+Introduction

answered