In this example, we will set up the JSON model within the controller and retrieve data from it.
We will be following below steps-
- 1. Import the JSONModel library in the controller file.
- 2. Pass the JSONModel on the main method.
- 3. Create a method that initializes the JSON model, adds data to it, and then retrieves the data.
Step 1 - Import the JSONModel library in the controller file
In controller.js file define the library in sap.ui.define.
So, sap.ui.define looks like below:
Step 2 - Pass the JSONModel on the main method
Step 3 - Create a method that initializes the JSON model, adds data to it, and then retrieves the data
In the controller.js file, define the below method-
So, the Controller will look like below:
And it is done !!!