Class: ConverterFactory

Oracle® JavaScript Extension Toolkit (JET)
5.2.0

E97691-01

QuickNav

Fields

ConverterFactory

Version:
  • 5.2.0
Since:
  • 0.6
Module:
  • ojvalidation-base

Constructor

(abstract) new ConverterFactory()

Contract for a ConverterFactory that provides a factory method to create a converter instance for the requested type. Factories handle the details of object creation. It allows the consumers of the factory to create specific converters without knowing the internals of the converter creation.

JET provides three factory implementations for number and datetime and color types that implement this contract.

Customers can register custom converter factories for the supported types or create and register factories for new types altogether.

See:
Examples

Get the ConverterFactory for 'datetime', and create a JET dateTime converter with specific options. See oj.DateTimeConverterFactory for what to set for options for the DateTime Converter.

var dateTimeCvtr = oj.Validation.converterFactory(oj.ConverterFactory.CONVERTER_TYPE_DATETIME);
var dateOptions = {day: 'numeric', month: 'numeric'};
var dayMonthConverter = dateTimeCvtr.createConverter(dateOptions);

Create a custom ConverterFactory with a new type and register that ConverterFactory.

// Define a new ConverterFactory for relative datetimes, like Today, Yesterday, Tomorrow
RelativeDateTimeConverterFactory = (function () {
  function _createRelativeDateTimeConverter(options)
  {
    // this is a custom converter 
    // See the Custom Converter JET demo for details on how to create 
    // your own Converter. Or see the Converter jsdoc.
    return new RelativeDateTimeConverter(options);
  }
  return {
    'createConverter': function (options) {
      return _createRelativeDateTimeConverter(options);
    }
  };
}());

// Register the custom factory with the new "relativeDate" type
oj.Validation.converterFactory("relativeDate", RelativeDateTimeConverterFactory);

// Get the custom factory using the new type.
var rdConverter =  oj.Validation.converterFactory("relativeDate")
.createConverter({relativeField: 'day', year: "numeric", month: "numeric", day: "numeric"});

Fields

(static) CONVERTER_TYPE_COLOR :string

Default type for a factory used to create color converters. This type is passed to the Validation.converterFactory method to retrieve the color converter factory of type oj.ColorConverterFactory.
Example

Create a JET color converter with options

this._convFactory = oj.Validation.converterFactory(oj.ConverterFactory.CONVERTER_TYPE_COLOR);
this._convHex  =  this._convFactory.createConverter({"format": "hex"})

(static) CONVERTER_TYPE_DATETIME :string

Default type for a factory used to create datetime converters. This type is passed to the Validation.converterFactory method to retrieve the datetime converter factory of type oj.DateTimeConverterFactory.
Example

Create a JET dateTime converter with options

var dateTimeCvtr = oj.Validation.converterFactory(oj.ConverterFactory.CONVERTER_TYPE_DATETIME);
var dateOptions = {day: 'numeric', month: 'numeric'};
var dayMonthConverter = dateTimeCvtr.createConverter(dateOptions);

(static) CONVERTER_TYPE_NUMBER :string

Default type for a factory used to create number converters. This type is passed to the Validation.converterFactory method to retrieve the number converter factory of type oj.NumberConverterFactory.
Example

Create a JET number converter with options

var convFactory = oj.Validation.converterFactory(oj.ConverterFactory.CONVERTER_TYPE_NUMBER);
var converter  =  convFactory.createConverter({
 style: 'currency', 
 currency: 'USD', 
 currencyDisplay: 'symbol', 
 pattern: '¤ ##,##0.00'});

Methods

createConverter(options) → {Object}

Creates an immutable converter instance of the type the factory implements.
Parameters:
Name Type Description
options Object | null an object literal containing properties required by the converter for its initialization. The properties provided in the options is implementation specific.
Throws:
if an unrecognized type was provided
Type
TypeError
Returns:
a converter instance.
Type
Object
Examples

Create a JET dateTime converter with options

var dateTimeCvtr = oj.Validation.converterFactory(oj.ConverterFactory.CONVERTER_TYPE_DATETIME);
var dateOptions = {day: 'numeric', month: 'numeric'};
var dayMonthConverter = dateTimeCvtr.createConverter(dateOptions);

Create your own ConverterFactory and Converter, register the Converter on your ConverterFactory, and use it when displaying relative date information on the page to the user.

----- Javascript -----
// Define new ConverterFactory
RelativeDateTimeConverterFactory = (function () {
  function _createRelativeDateTimeConverter(options)
  {
    // this is a custom converter See the Converter API or
    // Custom Converter JET demo for details on
    // how to create a custom converter.
    return new RelativeDateTimeConverter(options);
  }
  return {
    'createConverter': function (options) {
      return _createRelativeDateTimeConverter(options);
    }
  };
}());
// Register the custom factory with the new type
oj.Validation.converterFactory("relativeDate", RelativeDateTimeConverterFactory);
// Get the custom factory using the new type.
var rdConverter =  oj.Validation.converterFactory("relativeDate")
.createConverter({relativeField: 'day', year: "numeric", month: "numeric", day: "numeric"});
 ...
 // Our custom converter's format function returns an object with 'value' and 'title'.
 // We put the 'value' in innerHTML so the user can read it. E.g., Today or Tomorrow.
 // And we put the actual date in the title. The user can read it when they hover over
 // the word Today or Tomorrow.
 content = rdConverter.format(context.row.ScheduleFor);
 span.setAttribute('title', content.title);
 span.innerHTML = content.value;
...