Constructor
(abstract) new ConverterFactory()
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.
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 optionsObject | 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; ...