Please disable your adblock and script blockers to view this page

Search this blog

Wednesday, 24 April 2019

Oracle JET Common Model – Interacting With REST Web Services

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.

  1. <div class="oj-hybrid-padding">
  2. <h1>Customers Content Area</h1>
  3. <div>
  4. <oj-table id="table" summary="Employee List" aria-label="Employee Table"
  5. dnd='{"reorder": {"columns": "enabled"}}'
  6. scroll-policy='loadMoreOnScroll'
  7. scroll-policy-options='{"fetchSize": 10}'
  8. data='[[datasource]]'
  9. columns='[{"headerText": "Employee Id",
  10. "field": "EmployeeId"},
  11. {"headerText": "Employee Name",
  12. "field": "EmployeeName"},
  13. {"headerText": "Salary",
  14. "field": "EmployeeSalary"},
  15. {"headerText": "Age",
  16. "field": "EmployeeAge"}]'
  17. style='width: 100%; height: 400px;'>
  18. </oj-table>
  19. </div>
  20. </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

  1. /**
  2. * @license
  3. * Copyright (c) 2014, 2019, Oracle and/or its affiliates.
  4. * The Universal Permissive License (UPL), Version 1.0
  5. */
  6. /*
  7. * Your customer ViewModel code goes here
  8. */
  9. define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojtable', 'ojs/ojcollectiontabledatasource', 'ojs/ojknockout', 'ojs/ojmodel'],
  10. function (oj, ko, $) {
  11. function CustomerViewModel() {
  12. var self = this;
  13. //URL of REST Web Service
  14. self.serviceURL = 'http://dummy.restapiexample.com/api/v1/employees';
  15. //An observable to hold Employee Collection
  16. self.EmpCol = ko.observable();
  17. //An observable to show Employess in JET table using knockout data binding
  18. self.datasource = ko.observable();
  19. // Map attributes returned from REST Web service to view model attribute names
  20. self.parseEmp = function (response) {
  21. return {
  22. EmployeeId : response['id'],
  23. EmployeeName : response['employee_name'],
  24. EmployeeSalary : response['employee_age'],
  25. EmployeeAge : response['employee_salary']
  26. };
  27. };
  28. //ojModel to hold single employee record
  29. self.Employee = oj.Model.extend( {
  30. urlRoot : self.serviceURL,
  31. parse : self.parseEmp,
  32. idAttribute : 'EmployeeId'
  33. });
  34. self.emp = new self.Employee();
  35. //ojCollection to hold all employees
  36. self.EmpCollection = oj.Collection.extend( {
  37. url : self.serviceURL,
  38. model : self.emp,
  39. comparator : "EmployeeId"
  40. });
  41. self.EmpCol(new self.EmpCollection());
  42. //JET utility to convert ojCollection in row and column format
  43. self.datasource(new oj.CollectionTableDataSource(self.EmpCol()));
  44. }
  45. /*
  46. * Returns a constructor for the ViewModel so that the ViewModel is constructed
  47. * each time the view is displayed. Return an instance of the ViewModel if
  48. * only one instance of the ViewModel is needed.
  49. */
  50. return new CustomerViewModel;
  51. });

Now run and check the application



Cheers 🙂 Happy Learning