forio Toggle navigation

Converter Manager: Make your own Converters

Converters allow you to convert data -- in particular, model variables that you display in your project's user interface -- from one form to another.

Basic converting and formatting options are built in to Flow.js.

You can also create your own converters. Each converter should be a function that takes in a value or values to convert. To use your converter, register() it in your instance of Flow.js.

Methods

register

Add a new attribute converter to this instance of Flow.js.

Example

 Flow.dom.converters.register('max', function (value) {
     return _.max(value);
 }, true);

 Flow.dom.converters.register({
     alias: 'sig',
     parse: $.noop,
     convert: function (value) {
         return value.firstName + ' ' + value.lastName + ', ' + value.jobTitle;
 }, false);

 <div>
     The largest sales you had was <span data-f-bind="salesByYear | max | $#,###"></span>.
     The current sales manager is <span data-f-bind="salesMgr | sig"></span>.
 </div>

Parameters

  • alias: String|Function|Regex Formatter name.

  • converter: Function|Object If a function, converter is called with the value. If an object, should include fields for alias (name), parse (function), and convert (function).

  • acceptList: Boolean Determines if the converter is a 'list' converter or not. List converters take in arrays as inputs, others expect single values.

replace

Replace an already registered converter with a new one of the same name.

Parameters

  • alias: String Formatter name.

  • converter: Function|Object If a function, converter is called with the value. If an object, should include fields for alias (name), parse (function), and convert (function).

convert

Pipes the value sequentially through a list of provided converters.

Parameters

  • value: Any Input for the converter to tag.

  • list: Array|Object List of converters (maps to converter alias).

Return Value

  • Any: Converted value.

parse

Counter-part to convert(). Translates converted values back to their original form.

Parameters

  • value: String Value to parse.

  • list: String|Array List of parsers to run the value through. Outermost is invoked first.

Return Value

  • Any: Original value.