In the process of learning Oracle JET, this is another post about consuming REST Web Service using Oracle JET Common Model approach. Before going through this post I'll suggest you read my previous posts on Oracle JET.
Read more about JET Common Model and Collection
From the docs
The Oracle JET Common Model and Collection API provides a collection-of-records object model that includes classes for bringing external data into an Oracle JET application and mapping the data to the application’s view model.
The Oracle JET Common Model and Collection API provides a collection-of-records object model that includes the following classes:
oj.Model: Represents a single record from a data service such as a REST service
oj.Collection: Represents a set of data records and is a list of oj.Model objects of the same type
Here I am using the same JET nav drawer template application that is used in my first post - Using Oracle JET with JDeveloper
Add an oj-table component in the customers.html page and define the required number of columns to show employee details.
- <div class="oj-hybrid-padding">
- <h1>Customers Content Area</h1>
- <div>
- <oj-table id="table" summary="Employee List" aria-label="Employee Table"
- dnd='{"reorder": {"columns": "enabled"}}'
- scroll-policy='loadMoreOnScroll'
- scroll-policy-options='{"fetchSize": 10}'
- data='[[datasource]]'
- columns='[{"headerText": "Employee Id",
- "field": "EmployeeId"},
- {"headerText": "Employee Name",
- "field": "EmployeeName"},
- {"headerText": "Salary",
- "field": "EmployeeSalary"},
- {"headerText": "Age",
- "field": "EmployeeAge"}]'
- style='width: 100%; height: 400px;'>
- </oj-table>
- </div>
- </div>
Here goes the code of view model customers.js that reads data from web service and bind it to UI table. Read all comments to understand JS code
- /**
- * @license
- * Copyright (c) 2014, 2019, Oracle and/or its affiliates.
- * The Universal Permissive License (UPL), Version 1.0
- */
- /*
- * Your customer ViewModel code goes here
- */
- define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojtable', 'ojs/ojcollectiontabledatasource', 'ojs/ojknockout', 'ojs/ojmodel'],
- function (oj, ko, $) {
- function CustomerViewModel() {
- var self = this;
- //URL of REST Web Service
- self.serviceURL = 'http://dummy.restapiexample.com/api/v1/employees';
- //An observable to hold Employee Collection
- self.EmpCol = ko.observable();
- //An observable to show Employess in JET table using knockout data binding
- self.datasource = ko.observable();
- // Map attributes returned from REST Web service to view model attribute names
- self.parseEmp = function (response) {
- return {
- EmployeeId : response['id'],
- EmployeeName : response['employee_name'],
- EmployeeSalary : response['employee_age'],
- EmployeeAge : response['employee_salary']
- };
- };
- //ojModel to hold single employee record
- self.Employee = oj.Model.extend( {
- urlRoot : self.serviceURL,
- parse : self.parseEmp,
- idAttribute : 'EmployeeId'
- });
- self.emp = new self.Employee();
- //ojCollection to hold all employees
- self.EmpCollection = oj.Collection.extend( {
- url : self.serviceURL,
- model : self.emp,
- comparator : "EmployeeId"
- });
- self.EmpCol(new self.EmpCollection());
- //JET utility to convert ojCollection in row and column format
- self.datasource(new oj.CollectionTableDataSource(self.EmpCol()));
- }
- /*
- * Returns a constructor for the ViewModel so that the ViewModel is constructed
- * each time the view is displayed. Return an instance of the ViewModel if
- * only one instance of the ViewModel is needed.
- */
- return new CustomerViewModel;
- });
Now run and check the application
Cheers 🙂 Happy Learning