Please disable your adblock and script blockers to view this page

Search this blog

Monday, 27 May 2019

Oracle JET Common Model Factory Method to Consume REST Services

 Previously I have posted about consuming web services in Oracle JET using jQuery method and using Oracle JET Common Model. I have seen the Oracle JET Common Model Factory method in one of the blog and in MOOC course so tried the same but because of some version change or problem it was not working as expected but I got a solution in the OTN JET forum and sharing same here for reference.



Here I am using the same JET nav drawer template application that is used in my first post – Using Oracle JET with JDeveloper. and using ADF BC based REST web service

Add an oj-table component in the dashboard.html page and define the required number of columns to show Departments details.

  1. <div class="oj-hybrid-padding">
  2. <h1>Dashboard Content Area</h1>
  3. <div id="div1">
  4. <oj-table id="table" summary="Department List"
  5. aria-label="Departments Table"
  6. data='[[datasource]]'
  7. columns='[{"headerText": "Department Id",
  8. "field": "DepartmentId"},
  9. {"headerText": "Department Name",
  10. "field": "DepartmentName"},
  11. {"headerText": "Location Id",
  12. "field": "LocationId"},
  13. {"headerText": "Manager Id",
  14. "field": "ManagerId"}]'>
  15. </oj-table>
  16. </div>
  17. </div>

Next is to create a Factory JS file that holds the basic code for calling web service and read its response (Oracle JET Common Model and Collection is used in a separate file for better understanding of code )



Here goes the code for DeptFactory.js

  1. define(['ojs/ojcore'], function (oj) {
  2. var DeptFactory = {
  3. resourceUrl : 'http://127.0.0.1:7101/RestWebServApp-RESTWebService-context-root/rest/Jdev12.2.1/Department',
  4. // Single Department Model
  5. createDeptModel : function () {
  6. var dept = oj.Model.extend( {
  7. urlRoot : this.resourceUrl,
  8. idAttribute : "DepartmentId"
  9. });
  10. return new dept();
  11. },
  12. // Departments Collection
  13. createDepartmentsCollection : function () {
  14. var departments = oj.Collection.extend( {
  15. url : this.resourceUrl,
  16. model : this.createDeptModel(),
  17. comparator : "DepartmentId"
  18. });
  19. return new departments();
  20. }
  21. };
  22. return DeptFactory;
  23. });

And this Factory JS is used in dashboard.js View Model to populate data in the JET table

  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 dashboard ViewModel code goes here
  8. */
  9. define(['ojs/ojcore', 'knockout', 'jquery', 'DeptFactory', 'ojs/ojtable', 'ojs/ojcollectiontabledatasource'],
  10. function (oj, ko, $, DeptFactory) {
  11. function DashboardViewModel() {
  12. var self = this;
  13. self.deptCollection = DeptFactory.createDepartmentsCollection();
  14. self.datasource = ko.observable()
  15. // Setting collection in row and column format to show in JET table
  16. self.datasource(new oj.CollectionTableDataSource(this.deptCollection));
  17. }
  18. return new DashboardViewModel;
  19. });

Now run application and check, Department details are populated in the table from web service



So this is how we can use the Oracle JET Common Model Factory method to consume REST web services.

Cheers 🙂 Happy Learning