Oracle JET - Common Model

June 3, 2016    Oracle JET Common Model oj.Collections oj.Model Blog Post

Working with oj.Collection and oj.Model


As you build out your JET applications, you may need to work with data to populate a table, list or chart. If the data is in another system, it will probably require credentials or specific request headers before making a request. On top of that, when a successful request is complete you may want to manipulate the response to fit your data model. In Oraclet JET, all of this can be done by using the Oracle JET Common Model. In this post we take a quick look at oj.Collection and oj.Model.


Using the code

The Oracle JET Common Model and Collection API provides a way to represent a single record (oj.Model) and a set of data records in a list (oj.Collection) along with a set of methods for event handling (oj.Events). Basically, you can define what your data (model) looks like, then tell the collection to expect a series of these types of models when making a request. Then you can define events that know the specifics of your data/model.

Define the model

To get started, let’s define our model so that we can use it later when we create the collection.

self.Department = oj.Model.extend({
    urlRoot: self.serviceURL,
    parse: self.parseDept,
    idAttribute: 'DepartmentId'
});

The extend method of oj.Model provides a number of handy properties, like parse, parseSave and urlRoot. These properties are functions that are called by oj.Model when processing a response, for example:

self.parseDept = function (response) {
    return {
      Id: response['DeptId'],
      DeptCode:response['DepartmentCode'],
      Desc:response['DeptDesc']
    };
};

In the sample above, the parseDept function is a callback that takes the response and returns an object with fieldnames that match our model (instead of the fieldnames that are defined in the source system).

Create the collection

Now, let's create a collection. According to the documentation, collections represent a series of data records and a list of oj.Model objects of the same type.

// Create an instance of the model
self.myDept = new self.Department();

// Collection for departments self.DeptCollection = new oj.Collection.extend({ url: self.serviceURL, model: self.myDept });

Or, you can define the model inline

// Collection for departments
self.DeptCollection = new oj.Collection.extend({
   url: self.serviceURL,
   model: new oj.Model.extend({
     parse: self.parse,
     idAttribute: 'DeptId'
  })
});

One thing to note here is if the urlRoot attribute of oj.Model is not specified, the model will look to its collection. One or the other must be defined before CRUD operations can succeed.

Custom Headers

If you need custom headers for your request, create a function that returns an object with header values.

self.proxy = function(operation, collection, options) {
   var retObj = {};
   if(operation === 'read'){
      retObj['headers'] = {'prUrl': self.prUrl, 'pr-authoriztion': self.prAuthEncoded};
      retObj['mimeType'] = "text/plain";
   }
   return retObj;
};

Then set the customURL: property instead of urlRoot: for your collection

// Collection using custom headers
self.DeptCollection = new oj.Collection.extend({
    customURL: self.proxy,
    model: self.myDept
});


Code

See the Pen Oracle JET - oj.Collection


blog comments powered by Disqus